目录:/data/data/<packagename>/databases/
简单增删改查,创建表,升级表例子:
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 1);
// 将1变为2,升级,比之前的大就行,1,2,3,...,n
// dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
Button createButton = (Button) findViewById(R.id.create_button);
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.getWritableDatabase(); // 存储空间满,会异常
// dbHelper.getReadableDatabase(); // 不会异常,只读数据库
}
});
Button insertButton = (Button) findViewById(R.id.insert_button);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
// 开始组装第一条数据
values.put("name", "张3自传");
values.put("author", "张3");
values.put("pages", 454);
values.put("price", 16.44);
db.insert("Book", null, values); // 插入第一条数据
values.clear();
// 开始组装第二条数据
values.put("name", "李4自传");
values.put("author", "李4");
values.put("pages", 399);
values.put("price", 15.44);
db.insert("Book", null, values); // 插入第一条数据
}
});
Button updateButton = (Button) findViewById(R.id.update_button);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price", 10.11);
db.update("Book", values, "name = ?", new String[] {
"张3"
});
}
});
Button deleteButton = (Button) findViewById(R.id.delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?", new String[] {
"500"
});
}
});
Button selectButton = (Button) findViewById(R.id.select_button);
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book", null, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
// 遍历Cursor对象,取出数据并打印
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("xxd", name + author + pages + price);
} while (cursor.moveToNext());
}
}
});
}
}
以上提供的方法,可以用SQL语句代替:
-
insert:
db.execSQL("insert into Book (name, author, pages, price) values(?, ?, ?, ?)", new String[] {"title", "author", "453", "13.33"});
-
update:
db.execSQL("update Book set price = ? where name = ?", new String[] {"11.22", "author"});
-
delete:
db.execSQL("delete from Book where pages > ?", new String[] {"133"});
-
select:;
db.rawQuery("select * from Book", null);
作者:a191030148 发表于2017/2/18 23:43:54 原文链接
阅读:31 评论:0 查看评论