zoukankan      html  css  js  c++  java
  • sqlite-在Android中的使用sqlite-3

    sqlite-在Android中的使用sqlite-3

    ---------------------------------------------------------------------------------------------------------------------------------------------

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            //打开或创建test.db数据库
            SQLiteDatabase db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
            db.execSQL("DROP TABLE IF EXISTS person");
            //创建person表
            db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
            Person person = new Person();
            person.name = "james";
            person.age = 30;
            //插入数据
            db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[]{person.name, person.age});
            
            person.name = "lily";
            person.age = 20;
            //ContentValues以键值对的形式存放数据
            ContentValues cv = new ContentValues();
            cv.put("name", person.name);
            cv.put("age", person.age);
            //插入ContentValues中的数据
            db.insert("person", null, cv);
            
            cv = new ContentValues();
            cv.put("age", 35);
            //更新数据
            db.update("person", cv, "name = ?", new String[]{"james"});
            
            Cursor c = db.rawQuery("SELECT * FROM person WHERE age >= ?", new String[]{"33"});
            while (c.moveToNext()) {
                int _id = c.getInt(c.getColumnIndex("_id"));
                String name = c.getString(c.getColumnIndex("name"));
                int age = c.getInt(c.getColumnIndex("age"));
                Log.i("db", "_id=>" + _id + ", name=>" + name + ", age=>" + age);
            }
            c.close();
            
            //删除数据
            db.delete("person", "age < ?", new String[]{"35"});
            
            //关闭当前数据库
            db.close();
        }
  • 相关阅读:
    day 56
    Windows API 第16篇 GetLogicalDrivers 获取驱动器位掩码
    Windows API 第15篇 GetVolumeInformation 获取磁盘卷(驱动器)信息
    GetCommandLine CmdLineToArgvW
    Windows API 第14篇 DeleteAndRenameFile
    windows API 第13篇 MoveFileEx
    Windows API 第12篇 MoveFile
    _strupr _wcsupr _mbsupr
    复制字符串 _strdup _wcsdup _mbsdup
    windows API 第 11 篇 GetCurrentDirectory SetCurrentDirectory
  • 原文地址:https://www.cnblogs.com/harry335/p/4672856.html
Copyright © 2011-2022 走看看