以编程的方式使用SQLite数据库 执行CRUD(创建 读取 更新 删除)操作
DBAdapter数据库辅助类
package com.databses; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DBAdapter { static final String KEY_ROWID = "_id"; static final String KEY_NAME = "name"; static final String KEY_EMAIL = "email"; static final String TAG = "DBAdapter"; static final String DATABASE_NAME = "mydb"; static final String DATABASE_TABLE = "contacts"; static final int DATABASE_VERSION = 1; final Context context; //创建表sql语句 static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement,name text not null,email text not null)"; SQLiteDatabase db; DatabaseHelper dBHelper; public DBAdapter(Context context){ this.context = context; dBHelper = new DatabaseHelper(context); } //数据库辅助类 继承SQLiteOpenHelper private static class DatabaseHelper extends SQLiteOpenHelper{ public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } //创建数据库 @Override public void onCreate(SQLiteDatabase db) { try { db.execSQL(DATABASE_CREATE); } catch (Exception e) { e.printStackTrace(); } } //升级数据库版本 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "upgrading db from version "+oldVersion+"to"+newVersion+",which will destory all old data"); db.execSQL("drop table if exists contacts"); onCreate(db); } } //打开数据库 public DBAdapter open() throws SQLException { db = dBHelper.getWritableDatabase(); return this; } //关闭数据库 public void close(){ dBHelper.close(); } //插入一条联系人记录 public long insertContact(String name,String email){ ContentValues initalValues = new ContentValues(); initalValues.put(KEY_NAME,name); initalValues.put(KEY_EMAIL, email); return db.insert(DATABASE_TABLE, null, initalValues); } //删除一条记录 public boolean deleteContact(long rowId){ return db.delete(DATABASE_TABLE,KEY_ROWID+"="+rowId,null)>0; } //查询所有联系人 public Cursor getAllContacts() { return db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL},null, null, null, null, null, null); } //查询一条记录 public Cursor getContact(long rowId) throws SQLException{ Cursor cursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,KEY_EMAIL}, KEY_ROWID+"="+rowId, null, null, null, null, null); if(cursor != null){ cursor.moveToFirst(); } return cursor; } //更新一个联系人 public boolean updateContact(long rowId,String name,String email){ ContentValues contentValues = new ContentValues(); contentValues.put(KEY_NAME, name); contentValues.put(KEY_EMAIL, email); return db.update(DATABASE_TABLE, contentValues, KEY_ROWID+"="+KEY_ROWID, null)>0; } }
DatabaseActivity
package com.databses; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class DatabasesActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_databases); DBAdapter dbAdapter = new DBAdapter(this); dbAdapter.open(); dbAdapter.insertContact("weiwei", "[email protected]"); dbAdapter.insertContact("Jackson", "[email protected]"); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。