Android > SQLite

huanganxixi 2012-07-12

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("更新完了");
}

相关推荐

我的剑就是你的剑 / 0评论 2020-02-01