安卓 SQLite 示例

标签 android database sqlite sqliteopenhelper

我是 Android 新手,我必须创建一个需要使用 SQLite 数据库的应用程序。 我对 SQLite 的装备不太好。

能否告诉我SQLiteOpenHelper类的用途和用途,并提供一个简单的数据库创建和插入示例?

最佳答案

Sqlite 帮助类帮助我们管理数据库创建和版本管理。 SQLiteOpenHelper 负责所有数据库管理 Activity 。要使用它,
1.重写SQLiteOpenHelperonCreate(), onUpgrade()方法。可选择覆盖 onOpen() 方法。
2.使用该子类创建可读或可写数据库,并使用SQLiteDatabase的四个API方法insert()、execSQL()、update()、delete()创建、读取,更新和删除表格的行。

创建 MyEmployees 表并选择和插入记录的示例:

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "DBName";

    private static final int DATABASE_VERSION = 2;

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table MyEmployees
                                 ( _id integer primary key,name text not null);";

    public MyDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Method is called during creation of the database
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    // Method is called during an upgrade of the database,
    @Override
    public void onUpgrade(SQLiteDatabase database,int oldVersion,int newVersion){
        Log.w(MyDatabaseHelper.class.getName(),
                         "Upgrading database from version " + oldVersion + " to "
                         + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS MyEmployees");
        onCreate(database);
    }
}

现在你可以像下面这样使用这个类了,

public class MyDB{  

    private MyDatabaseHelper dbHelper;  

    private SQLiteDatabase database;  

    public final static String EMP_TABLE="MyEmployees"; // name of table 

    public final static String EMP_ID="_id"; // id value for employee
    public final static String EMP_NAME="name";  // name of employee

    /** 
     * 
     * @param context 
     */  
    public MyDB(Context context){  
        dbHelper = new MyDatabaseHelper(context);  
        database = dbHelper.getWritableDatabase();  
    }


    public long createRecords(String id, String name){  
        ContentValues values = new ContentValues();  
        values.put(EMP_ID, id);  
        values.put(EMP_NAME, name);  
        return database.insert(EMP_TABLE, null, values);  
    }    

    public Cursor selectRecords() {
        String[] cols = new String[] {EMP_ID, EMP_NAME};  
        Cursor mCursor = database.query(true, EMP_TABLE,cols,null  
            , null, null, null, null, null);  
        if (mCursor != null) {  
            mCursor.moveToFirst();  
        }  
        return mCursor; // iterate to get each value.
    }
}

现在您可以在 Activity 中使用 MyDB 类来进行所有数据库操作。创建记录将帮助您插入值,类似地您可以拥有自己的更新和删除函数。

关于安卓 SQLite 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12015731/

相关文章:

SQLite:增加唯一整数字段

Android Webview与menudrawer一起使用时的渲染

android - 我确保字符串完整性的方法安全吗?

android - 如何遍历字符串数组并添加到 Realm 数据库?

c# - 使用SQLite DB在C#中创建Microsoft报表

Android 错误将位置数据从 LocationListener 类写入 SQLite 数据库

android - 在android项目上运行 Sonar : The class 'foo.bar.R$string' could not be matched to its original source file

java - 使用 Java 和 Sax 解析某些 RSS 提要时遇到问题

SQL检查列名后输入参数值

php - 如果数据是唯一的,则将数据添加到数据库