SQLite是一个小型的内嵌于Android的数据库;我们不需要引入驱动即可访问它;
SQLite可视化工具:SQLite Expert Professional 3;
package org.xiazdong.db.util; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseHelper extends SQLiteOpenHelper{ private static final String TAG = "DatabaseHelper"; private static int VERSION = 1; public DatabaseHelper(Context context) { super(context, "test.db", null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table 语句"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i(TAG, "版本更新..."); } }
ContentValues values = new ContentValues(); values.put("name","xiazdong"); values.put("age",20); db.insert("tablename",null,values);
db.exeSQL("update person set age=? where name=?",new Object[]{30,"xiazdong"});
ContentValues values = new ContentValues(); values.put("age",30);/*set子句*/ db.update("tablename",values,"name=?"/*where子句*/,new String[]{"xiazdong"}/*where子句参数*/);
Cursor cursor = db.rawQuery("select * from person where name=?",new Object[]{"xiazdong"}); while(cursor.moveToNext()){ //int index = cursor.getColumnIndex(String name);//根据name获得索引 //String name = cursor.getString(int index); //根据索引获得值 String name = cursor.getString(cursor.getColumnIndex("name")); }
Cursor cursor = db.rawQuery("select count(*) from person", null); cursor.moveToFirst(); int count = cursor.getInt(0);
db.beginTransaction(); try{ //事务操作 db.setTransactionSuccessful();//此句必须要有,不然db.endTransaction()默认为回滚 } finally{ db.endTransaction(); }