|
第一部分:
1.SQLiteDatabase简介
应用程序获得了代表数据库的SQLiteDatabase对象,那么就可以通过SQLiteDatabase对象来操作数据库了。
1)打开对应的数据库
<wbr><wbr>static SQLiteDatabase openDatabase(Stsering path,SQLiteDatabase.CursorFactory factory,int flags)</wbr></wbr>
<wbr><wbr>static SQLiteDatabase openOrCreateDatabase(String path,SQLiteDatabase.CursorFactory factory)</wbr></wbr>
<wbr><wbr>static SQLiteDatabase openOrCreateDatabase(File file,SQLiteDatabase.CursorFactory factory)</wbr></wbr>
<wbr></wbr>
2)操作数据库
<wbr><wbr>execSQL(String sql,Object[] bindArgs):执行带占位符的SQL语句</wbr></wbr>
<wbr><wbr>execSQL(String sql)</wbr></wbr>
<wbr><wbr>insert(String table,String nullColumnHack,ContentValues values)</wbr></wbr>
<wbr><wbr>update(String table,ContentValues values,String whereClause,String[] whereArgs)</wbr></wbr>
<wbr><wbr>delete(String table,String whereClase,String[] whereArgs)</wbr></wbr>
<wbr><wbr>Cursor query(boolean distinct,String table,String[] columns,String selection,String[]</wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>selecionArgs,String groupBy,String,having,String orderBy,String limit);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>rawQuery(String sql,String[] selectionArgs)</wbr></wbr>
<wbr><wbr>beginTransaction() 启动事务</wbr></wbr>
<wbr><wbr>endTransaction() 结束事务</wbr></wbr>
<wbr></wbr>
以上查询方法返回的都是一个Cursor对象,Cursor提供了如下方法移动查询结果的记录指针。
move(int offset)
boolean moveToFirst()
boolean moveToLast()
boolean moveToNext()
boolean moveToPosition(int position)
boolean moveToPrevious(),将指针移动到上一行,如果成功则返回true。
第二部分:
1.创建数据库和表
如:SQLiteDatabase.openCrreateDatabase("/mnt/db/temp.db3",null);
2.插入、更新、删除 使用execSQL()方法,而查询的时候,使用rawQuery()方法,查询的结果一般用Cursor对象保存。
3.在程序结束的时候,需要关闭SQLiteDatabase,否则会导致资源泄漏。db.close();
4.SimpleCursorAdapter的构造函数与SimpleAdapter的构造器参数大致相同,区别是SimpleAdapter封装的是Map对象,而SimpleCursorAdapter封装的是Cursor对象。
如:
<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">SimpleCursorAdapteradapter = new SimpleCursorAdapter(</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">DBTest.this,R.layout.line,cursor,newString[]{"列名","列名"},new</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">int[]{R.id.my_title,R.id.my_content});</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">需要注意的是,使用SimpleCursorAdapter封装Cursor是要求底层数据表的主键列的列名是_id,因为SimpleCursorAdapter只能识别列名为_id的主键。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">5.sqlite3工具</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">1)</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">.database</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">.tables</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">.help</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">2)SQLite内部只支持5种数据类型:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">NULL,INTEGER,REAL(浮点数),TEXT(文本),BLOB(大二进制对象)</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">但实际上SQLite完全可以接受varchar(),char(),decimal()等数据类型,只不过会转化成上面的5种类型罢</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">了。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">3)SQLite它允许把各种数据类型的数据保存到任何类型的字段中,开发者不用关心声明该字段所使用的数据类</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">类型。如:程序可以把字符串类型的值存入到Integer类型的字段中,也可以把数值类型的值存入到布尔类</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">类型中。</span><span style="word-wrap:normal; word-break:normal; line-height:21px; font-family:simsun; font-size:14px; color:rgb(237,28,36)">但是</span><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">有一种情况除外,定义为INTEGERPRIMARY KEY 的字段只能存储为64为整数。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">由于SQLite允许存入数据时忽略底层数据列实际的数据类型,故在编写构建表的语句时,可以省略列后面的</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">类型声明。如:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">createtable my_test(</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">_idinteger primary key autoincrement,</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">name,</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">pass</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">)</span><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
第三部分:使用特定方法操作数据库
1.insert(String table,String nullColumnHack,ContentValues values)
<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">table:表名</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">nullColumnHack代表强行插入null值的数据列的列名</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">values:代表一行记录的数据</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">ContentValues类似于Map,他提供了put(Stringkey,XXX value) 方法存入数据,getAsXXX(String key)</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">方法取得数据,key为数据列的列名。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">如代码:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">ContentValuesvalues = new ContentValues();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">values.put("name","adfasdf");</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">values.put("age",50);</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">longrowid = db.insert("person_inf",null,values);</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">不管第三个参数是否包含数据,都会添加一个记录,如果第三个参数为空,则添加一条除主键外都为空的</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">记录。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">2.update(String table,ContentValues,String whereClase,String[] whereArgs)</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">whereClase满足whereClase字句会被更新</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">whereArgs为whereClase传递参数</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">如:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">ContentValuesvalues = new ContentValues();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">values.put("name","新人名");</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">intresult = db.update("person_info",values,"_id>?",new Integer[]{ 20});</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">3.delete(String table,String whereClase,String[] whereArgs)</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">4.query()方法</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">5.事务</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">beginTransaction()开始事务</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">endTransaction()结束事务</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">判断上下文是否在事务环境中,inTransaction()</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">结束事务有提交事务和回滚事务,取决于SQLiteDatabase的setTransactionSuccessful</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">()来标注</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">事务。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">如:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">db.beginTransaction();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">try{</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">db.setTransactionSuccessful</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">}finally{</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">db.endTransaction();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px">}</span><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
第四部分:SQLiteOpenHelper类
SQLiteOpenHelper是管理数据库的工具类。
1.onCreate()
<wbr><wbr>用于初次使用软件时生成数据库表。当调用getReadableDatabase()或getWritableDatabase()方法时,<br><wbr><wbr>如果数据库不存在,android系统便会自动生成一个数据库,接着调用onCreate()方法。<br><wbr><wbr>故可以在onCreate()方法里生成表的一些结构和添加一些初始化的数据。<br><br>2.onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)<br><wbr><wbr><br>3.getReadableDatabase() 和getWritableDatabase()区别<br><wbr><wbr><wbr><wbr>getWritableDatabase()以读写的方式打开数据库,如果数据库磁盘满了,那么数据库只能读不能写。若使<br><wbr><wbr><wbr><wbr>用getWritableDatabase()则会出错<br><wbr><wbr><wbr><wbr>getReadableDatabase()以读写的方式打开数据库,如果数据库磁盘满了,就会打开失败,打开失败后,继<br><wbr><wbr><wbr><wbr>序尝试去只读的方式打开数据库。<br><br><wbr><br>使用心得:</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>1)创建一个类继承与SQLiteOpenHelper</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>构造函数</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>onCreate(SQLiteDatabase db)</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>2) 通过1)步中的类创建SQliteOpenHelper子类的对象dbHelper</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>获得操作数据库对象dbHelper.getReadableDatabase()(或者 getWritableDatabase())</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>3) 将查询的结果cursor 转化为ArrayList类型</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>ArrayList<Map<String,String>> result = new ArrayList<Map<String,String>>;</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>while(cursor.moveToNext()){</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Map<String,String> map= new Map<String,String>();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>map.put("key",cursor.getString(1));</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>result.add(map);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>return result;</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>4)在结束的时候,需要重写onDestory()方法</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>判断dbHelper 是否为null,不等于则要dbHelper.close()</wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>5)SimpleAdapter adapter = new SimpleAdapter(</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>XXActivity.this,arraylist,R.layout.line,new String[]{"key1","key2"},new</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int[]{R.id.x1,R.id.x2}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr>);</wbr></wbr></wbr></wbr></wbr></wbr> |
|