Android > SQLite
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //打开一个数据库,如果没有 则 create SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null); //execSQL 执行一个非SELECT和不返回数据 的SQL 语句l//// //db.execSQL("DROP TABLE IF EXISTS person"); //删除一个数据库表 如果存在 //创建person表 如果不存在 person 表 db.execSQL("CREATE TABLE IF NOT EXISTS person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)"); //ContentValues 以 键值 的形式存放数据 ContentValues cv = new ContentValues(); cv.put("name", "sssssssss"); cv.put("age", 9999); //db.insert("person", null, cv); //更新数据 类似, UPDATA person cv, WHERE name = "sssssssss" ; //db.update("person", cv, "name = ?", new String[]{"sssssssss"}); // 删除数据 类似, DELETE person WHERE age = "9999" //db.delete("person","age = ?",new String[]{"9999"}); //查询 1条, WHERE age = "9999" //Cursor c = db.rawQuery("SELECT * FROM person WHERE age >= ?", new String[]{"9999"}); //查询多条 Cursor c = db.rawQuery("SELECT * FROM person", null); //循环输出查询结果 while (c.moveToNext()) { int _id = c.getInt(c.getColumnIndex("_id")); String name = c.getString(c.getColumnIndex("name")); int age = c.getInt(c.getColumnIndex("age")); System.out.println("_id=>" + _id + ", name=>" + name + ", age=>" + age); } System.out.println("更新完了"); }
相关推荐
specialbrian 2020-07-31
loveandroid0 2020-06-18
DAV数据库 2020-06-17
URML 2020-06-13
Dlanguage 2020-06-12
Plant 2020-06-07
疯狂老司机 2020-06-07
chibangyuxun 2020-06-07
sdwylry 2020-06-04
airfling 2020-05-31
Plant 2020-05-31
zbcaicai 2020-05-26
chaochao 2020-05-19
Plant 2020-05-17
小火车 2020-05-14
beibeijia 2020-04-25
Rain 2020-04-16