Android 数据库操作 创建 添加 删除 查询
jopen
12年前
第一步创建数据库工具类:
import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper { DBConnection helper; public DBHelper(Context ctx) { helper = new DBConnection(ctx); } public DBHelper() { } public void add(ContentValues values) { SQLiteDatabase db = helper.getWritableDatabase(); db.insert(UserSchema.TABLE_NAME, null, values); db.close(); } public void DropTable(String TB_NAME) { try { SQLiteDatabase db = helper.getWritableDatabase(); String sql = "DROP TABLE " + TB_NAME; db.execSQL(sql); } catch (Exception ex) { } } public void DropDatabase(Context ctx, String DATABASE_NAME) { try { ctx.deleteDatabase(DATABASE_NAME); } catch (Exception ex) { } } public void update(ContentValues values, String where, String[] whereArgs) { SQLiteDatabase db = helper.getWritableDatabase(); db.update(UserSchema.TABLE_NAME, values, where, whereArgs); db.close(); } public void delete(String where, String[] whereArgs) { SQLiteDatabase db = helper.getWritableDatabase(); db.delete(UserSchema.TABLE_NAME, where, whereArgs); db.close(); } public void delDB(String DBName) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("DROP Database " + DBName); db.close(); } public List query(String where, String[] whereArgs, String orderBy, String limit) { try { final SQLiteDatabase db = helper.getReadableDatabase(); Cursor c = null; c = db.query(UserSchema.TABLE_NAME, null, where, whereArgs, null, null, orderBy, limit); List lst = new ArrayList(); while (c != null && c.moveToNext()) { CItem item = new CItem(); item.setID(c.getString(c.getColumnIndex("m_key"))); item.setValue(c.getString(c.getColumnIndex("m_value"))); lst.add(item); } if (c != null)c.close(); return lst; } catch (Exception e) { // TODO: handle exception } return null; } public interface UserSchema { String TABLE_NAME = "movement"; String ID = "m_id"; String TYPE = "m_type"; String Key = "m_key"; String Value = "m_value"; } public static class DBConnection extends SQLiteOpenHelper { private static final String DATABASE_NAME = "SP.db"; private static final int DATABASE_VERSION = 1; private DBConnection(Context ctx) { super(ctx, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String create_sql = "CREATE TABLE movement(m_id integer primary key autoincrement,m_type varchar(10)," + "m_key varchar(20),m_value varchar(100),m_order integer)"; db.execSQL(create_sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub // String alter_sql = "ALTER TABLE movement"; // db.execSQL(alter_sql); } } } 使用数据库如下代码: public DBHelper getDb() { if (db == null) db = new DBHelper(this); return db; } 添加: ContentValues cv = new ContentValues(); cv.put("m_type", type); cv.put("m_key", key); cv.put("m_value", value); getDb().add(cv); 查询: List lst = getDb().query("m_type=?", new String[] { cmd }, null, null);
创建后的数据库在
cd datat/data/当前项目包名/databases
</citem></citem>