例子挺简单,不废话,贴代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建数据库,一个应用只能有一个数据库
SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
// 创建表
db.execSQL("create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null)");
// 清空表中的所有行 或者 db.delete("usertb", null, null);
// db.delete("usertb", "_id>?", new String[]{"0"});
db.delete("usertb", null, null);
// 插入数据
ContentValues values = new ContentValues();
values.put("name", "张三");
values.put("age", 18);
values.put("sex", "男");
db.insert("usertb", null, values);
values.clear();
values.put("name", "李四");
values.put("age", 19);
values.put("sex", "男");
db.insert("usertb", null, values);
values.clear();
values.put("name", "王五");
values.put("age", 18);
values.put("sex", "女");
db.insert("usertb", null, values);
values.clear();
// 更新表里的内容
values.put("sex", "男");
db.update("usertb", values, "name like ?", new String[] { "%王%" });
values.clear();
// 查询表里的内容
Cursor cursor = db.query("usertb", null, "_id>?", new String[] { "0" },
null, null, "_id");
if (cursor != null) {
while (cursor.moveToNext()) {
String[] columnNames = cursor.getColumnNames();
for (String columnName : columnNames) {
Log.i("Main",
cursor.getString(cursor.getColumnIndex(columnName)));
}
}
cursor.close();
}
db.close();
// SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
// db.execSQL("create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )");
//
// db.execSQL("insert into usertb(name,sex,age) values('张三','女',18)");
// db.execSQL("insert into usertb(name,sex,age) values('李四','女',19)");
// db.execSQL("insert into usertb(name,sex,age) values('王五','男',20)");
//
// Cursor c = db.rawQuery("select * from usertb", null);
// if (c != null) {
// while (c.moveToNext()) {
// Log.i("info", "_id:" + c.getInt(c.getColumnIndex("_id")));
// Log.i("info", "name:" + c.getString(c.getColumnIndex("name")));
// Log.i("info", "age:" + c.getInt(c.getColumnIndex("age")));
// Log.i("info", "sex:" + c.getString(c.getColumnIndex("sex")));
// Log.i("info", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// }
// c.close();
// }
// db.close();
}
}
1.openOrCreateDatabase("user.db", MODE_PRIVATE, null); 创建一个名为user的数据库
方法原型:public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory)
很明显该方法返回的是一个SQLiteDatabase 对象,参数:
MODE_PRIVATE
for the default operation,
MODE_WORLD_READABLE
and
MODE_WORLD_WRITEABLE
to control permissions. Use
MODE_ENABLE_WRITE_AHEAD_LOGGING
to enable write-ahead logging by default.
2.db.execSQL('"")
方法原型:public void execSQL (String sql)
参数:sql | the SQL statement to be executed. Multiple statements separated by semicolons are not supported. |
---|
3.db.insert("usertb", null, values);
方法原型:public long insert (String table, String nullColumnHack, ContentValues values)
-
Parameters:
- table the table to insert the row into
-
nullColumnHack optional; may be
null
. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your providedvalues
is empty, no column names are known and an empty row can't be inserted. If not set to null, thenullColumnHack
parameter provides the name of nullable column name to explicitly insert a NULL into in the case where yourvalues
is empty. - values this map contains the initial column values for the row. The keys should be the column names and the values the column values Returns:
- the row ID of the newly inserted row, or -1 if an error occurred
方法原型:public int delete (String table, String whereClause, String[] whereArgs)
Parameters
table | the table to delete from |
---|---|
whereClause | the optional WHERE clause to apply when deleting. Passing null will delete all rows. |
whereArgs | You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings. |
Returns
- the number of rows affected if a whereClause is passed in, 0 otherwise. To remove all rows and get a count pass "1" as the whereClause.