Android的SQLite的实例 [英] Android SQLite Example

查看:136
本文介绍了Android的SQLite的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,我要创造,我需要使用的SQLite数据库的应用程序。 我不是这么好装备SQLite的。

I am new to Android and I have to create an application where I need to use a SQLite database. I am not so well equipped with the SQLite.

您能告诉我是什么目的和用途的 SQLiteOpenHelper 类,并提供一个简单的数据库创建和插入的例子吗?

Could you tell me what the purpose and use of the SQLiteOpenHelper class, and provide a simple database creation and insertion example?

推荐答案

sqlite的辅助类可以帮助我们来管理数据库的创建和版本管理。 SQLiteOpenHelper需要的所有数据库管理活动的照顾。要使用它,     
1.Override 的onCreate(), SQLiteOpenHelper 的onUpgrade()的方法。可选择覆盖的OnOpen()方法。     
2.使用这个子类来创建无论是读写数据库,并使用SQLiteDatabase四个API方法插入(),execSQL(),更新(),删除()创建,读取,更新和删除行的表。

Sqlite helper class helps us to manage database creation and version management. SQLiteOpenHelper takes care of all database management activities. To use it,
1.Override onCreate(), onUpgrade() methods of SQLiteOpenHelper. Optionally override onOpen() method.
2.Use this subclass to create either a readable or writable database and use the SQLiteDatabase's four API methods insert(), execSQL(), update(), delete() to create, read, update and delete rows of your table.

实例创建一个MyEmployees表,并选择和插入记录:

Example to create a MyEmployees table and to select and insert records:

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);
    }
}

现在你可以使用这个类,如下,

Now you can use this class as below,

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.
}
}

现在你可以使用的MyDB类,你的活动让所有的数据库操作。在创建记录将有助于你插入值,同样可以有自己的功能更新和删除。

Now you can use MyDB class in you activity to have all the database operations. The create records will help you to insert the values similarly you can have your own functions for update and delete.

这篇关于Android的SQLite的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆