本文參考郭霖《第一行代碼》第2版
public class DBActivity extends AppCompatActivity {
private static final String TAG = "DBActivity";
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_db);
dbHelper = new MyDatabaseHelper(DBActivity.this, "BookStore.db", null, 4);
//綁定view
Button createDatabase = findViewById(R.id.create_database);
Button addData = findViewById(R.id.add_data);
Button updateData = findViewById(R.id.update_data);
Button deleteData = findViewById(R.id.delete_data);
Button queryData = findViewById(R.id.query_data);
//設(shè)置監(jiān)聽器
createDatabase.setOnClickListener(v -> {
//創(chuàng)建數(shù)據(jù)庫
dbHelper.getWritableDatabase();
});
//添加數(shù)據(jù)
addData.setOnClickListener(v -> {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
//開始裝第一組數(shù)據(jù)
values.put("name", "第一行代碼");
values.put("author", "guolin");
values.put("pages", 570);
values.put("price", 30);
db.insert("Book", null, values);
values.clear();
//開始裝第二組數(shù)據(jù)
values.put("name", "瘋狂Android講義");
values.put("author", "ligang");
values.put("pages", 510);
values.put("price", 50.5);
db.insert("Book", null, values);
});
//更新數(shù)據(jù)
updateData.setOnClickListener(v -> {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", 9.99);
db.update("Book", values, "name = ?", new String [] {"第一行代碼"});
});
//刪除數(shù)據(jù)
deleteData.setOnClickListener(v -> {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages < ?", new String[]{"550"});
});
//查詢數(shù)據(jù)
queryData.setOnClickListener(v -> {
SQLiteDatabase db = dbHelper.getWritableDatabase();
//查詢Book表中所有的數(shù)據(jù)
Cursor cursor = db.query("Book", null, null, null, null, null, null);
if (cursor.moveToFirst()){
do {
//遍歷Cursor對象棘脐,取出數(shù)據(jù)并打印
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d(TAG, "book name is " + name);
Log.d(TAG, "book author is " + author);
Log.d(TAG, "book pages is " + pages);
Log.d(TAG, "book price is " + price);
}while (cursor.moveToNext());
}
cursor.close();
});
}
}
下面是繼承SQLiteOpenHelper的數(shù)據(jù)庫幫助類的源代碼:
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book ("
+ "id integer primary key autoincrement, "
+ "author text, "
+ "price real, "
+ "pages integer, "
+ "name text)";
public static final String CREATE_CATEGORY = "create table Category ("
+ "id integer primary key autoincrement, "
+ "category_name text, "
+ "category_code integer)";
private Context mContext;
public MyDatabaseHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
備注:用adb查看db文件:
adb shell
if (手機root ) 會顯示 # else 會顯示$
如果沒有root 執(zhí)行 su root
cd /data/data/packagename/databases
sqlite3 BookStore.db
.table //查看所有表
.schema //查看建表語句
特別注意:查詢語句最后需要加“卵贱;”
select * from Book;