加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite

发布时间:2020-12-13 00:01:37 所属栏目:百科 来源:网络整理
导读:SQLite 2个主要的类 A)__SQLiteOpenHelper 这个类是一个抽象类 实现他的类必须包含一个构造方法: public **(Context context,表的名字,CursorFactory,版本){spuer(**,**,**)} 参数注意: version(版本):必须是大于0的整数 方法: getReadableDatabase() 返回:

SQLite
2个主要的类
A)__SQLiteOpenHelper
这个类是一个抽象类
实现他的类必须包含一个构造方法: public **(Context context,表的名字,CursorFactory,版本){spuer(**,**,**)}
参数注意:
version(版本):必须是大于0的整数

方法:
getReadableDatabase() 返回:可读的SQLiteDatabase
getWritableDatabase() 返回:可写的SQLiteDatabase
onCreate(SQLiteDatabase db) 第一次调用这个类的时候会调用,是个回调函数
onUpgradel(SQLiteDatabase db,int oldVersion,int new Version) 升级数据库的时候调用,sans-serif; font-size:14px; line-height:25px">claose() 当关闭数据库时,sans-serif; font-size:14px; line-height:25px">B)__SQLiteDatabase SQLite操作的类
实例:
//Databasehelper 是一个继承自SQLiteOpenHelper的类
Databasehelper dbHeper = new DatabaseHelper(上下文环境,"数据库名称",null,版本);
SQLiteDatabase db = dbHeper.getReadableDatabase();//或 getWritableDatabase()
//查询 返回 Cursor(一个游标类型,可以用 curosr.moveToNext()执行下一步操作)
Curosr cu = db.query(table,columns,selection,selectionArgs,groupBy,having,orderBy);
while(cu.moveToNext()){
String name =cu.getString(cu.getColumnINdex("列名"));
}
//增加
ContentValues cv = new ContentValues();
cv.put("列名","值");
long row = db.insert("表名",cv);
//删除
String where ="ID = ?";
String [] whereValue = {Integer.toString(id)};//这里如果where有多少个? 就要有多少个值
db.delete("表名",where,whereValue);
//修改
String where ="id =?";
String [] whereValue = {Integer.toString(id)};

db.update("表名",cv,whereValue);

  1. importandroid.content.ContentValues;
  2. importandroid.content.Context;
  3. importandroid.database.Cursor;
  4. importandroid.database.sqlite.SQLiteDatabase;
  5. importandroid.database.sqlite.SQLiteOpenHelper;
  6. importandroid.util.Log;
  7. publicclassDatabaseAdapter{
  8. privatestaticfinalStringDB_NAME="Test.db";//数据库名
  9. finalintDB_VERSION=1;//数据库版本
  10. finalStringDB_TABLE="my_order";//表名
  11. finalStringKEY_ID="_id";//id
  12. finalStringKEY_ORDER_ID="order_id";//订单号
  13. finalStringKEY_TYPE="_type";//订单类型
  14. finalStringKEY_STATE="_state";//订单状态
  15. privateContextcontext;
  16. privateDatabaseHelpermDatabaseHelper;
  17. privateSQLiteDatabasemSQLiteDatabase;
  18. classDatabaseHelperextendsSQLiteOpenHelper{
  19. //创建数据库语句
  20. finalStringDB_CREAT="CREATETABLE"
  21. +DB_TABLE
  22. +"("+KEY_ID+"INTEGERPRIMARYKEY,"
  23. +KEY_ORDER_ID+"TEXT,250); margin:0px 0px 0px 38px; padding-left:10px; padding-right:0px; font-size:1em; padding-top:0px"> +KEY_TYPE+"INTEGER,250); margin:0px 0px 0px 38px; padding-left:10px; padding-right:0px; font-size:1em; padding-top:0px"> +KEY_STATE+"INTEGER)";
  24. publicDatabaseHelper(Contextcontext){
  25. super(context,DB_NAME,null,DB_VERSION);
  26. }
  27. @Override
  28. voidonCreate(SQLiteDatabasedb){
  29. //TODOAuto-generatedmethodstub
  30. db.execSQL(DB_CREAT);
  31. voidonUpgrade(SQLiteDatabasedb,85); font-weight:bold">intoldVersion,85); font-weight:bold">intnewVersion){
  32. db.execSQL("DROPTABLEIFEXISTS"+DB_TABLE);
  33. onCreate(db);
  34. publicDatabaseAdapter(Contextcontext){
  35. this.context=context;
  36. //开启
  37. voidopen(){
  38. mDatabaseHelper=newDatabaseHelper(context);
  39. mSQLiteDatabase=mDatabaseHelper.getWritableDatabase();
  40. //关闭
  41. voidclose(){
  42. mSQLiteDatabase.close();
  43. mDatabaseHelper.close();
  44. //增
  45. longinsertData(StringorderId,85); font-weight:bold">inttype){
  46. ContentValuesvalues=newContentValues();
  47. values.put(KEY_ORDER_ID,orderId);
  48. values.put(KEY_TYPE,type);
  49. values.put(KEY_STATE,Config.STATE_APPLY);
  50. longid=mSQLiteDatabase.insert(DB_TABLE,KEY_ID,values);
  51. returnid;
  52. //删
  53. booleandeleteData(Contextcontext,85); font-weight:bold">longid){
  54. booleandelete=mSQLiteDatabase.delete(DB_TABLE,KEY_ID+"="+id,85); font-weight:bold">null)>0;
  55. returndelete;
  56. //改
  57. booleanupdateData(longid,85); font-weight:bold">intstate){
  58. ""+state);
  59. booleanupdate=mSQLiteDatabase.update(DB_TABLE,values,85); font-weight:bold">returnupdate;
  60. //查
  61. publicCursorfetchData(Stringselection){
  62. CursormCursor=mSQLiteDatabase.query(DB_TABLE,newString[]{KEY_ID,KEY_ORDER_ID,KEY_TYPE,KEY_STATE},85); font-weight:bold">null);
  63. if(mCursor!=null)
  64. mCursor.moveToFirst();
  65. returnmCursor;
  66. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读