利用Android中的SQLite进行CRUD
一、SQLite?
SQLite是一种轻量级的数据库,Android中自带有。
android可以借助SQLiteOpenHelper来对SQLite进行操作
二、效果图
三、代码编写与解析
1.编写操作系统的工具类DBHelper继承SQLiteOpenHelper
SQLiteOpenHelper是用来管理数据库的创建和版本的管理
package linys.util; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; /** * * @Project: Android_SQLite * @Desciption: * SQLiteOpenHelper * A helper class to manage database creation and version management * SQLiteOpenHelper 管理数据库的创建和版本的管理 * @Author: LinYiSong * @Date: 2011-3-25~2011-3-25 */ public class DBHelp extends SQLiteOpenHelper { /** * * @param context * @param name数据库文件名 * @param factory * @param version */ public DBHelp(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /** * Called when the database is created for the first time. * This is where the creation of tables and the initial population of the tables should happen */ @Override public void onCreate(SQLiteDatabase db) { System.out.println("SQLiteDatabase's OnCreate"); db.execSQL("create table user(id int,name varchar(20))"); } /** * upgrade:升级,数据库版本从1-->2会调用这个函数 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("upgrade"); } }
2.编写Activity,对SQLite数据库进行操作
package linys.db; import linys.util.DBHelp; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * * @Project: Android_SQLite * @Desciption: SQLite是一种轻量级的数据库 * android可以借助SQLiteOpenHelper来对SQLite进行操作: * * 操作类,继承SQLiteOpenHelper如DBHelp * * * adb:android debug bridge * android自带的调试工具,可以进入内核,进行操作 * adb shell 进入内核 * 编写的程序放在data/data对于的包(即我们在new出Program时指定的package * * 如果程序有用到数据库,则数据库在database下 * 对数据进行操作前,先切换的sqlite3模式下:sqlite3 数据库名 *.schema可以查看数据库中创建的表格语句, * * @Author: LinYiSong * @Date: 2011-3-10~2011-3-10 */ public class SQLiteDemo extends Activity { /** Called when the activity is first created. */ private Button createDataBaseBtn; private Button updateDataBaseBtn; private Button insertBtn; private Button updateBtn; private Button queryBtn; private Button deleteBtn; DBHelp dbHelp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createDataBaseBtn=(Button)this.findViewById(R.id.createDatabase); updateDataBaseBtn=(Button)this.findViewById(R.id.updateDatabase); insertBtn=(Button)this.findViewById(R.id.insert); updateBtn=(Button)this.findViewById(R.id.update); queryBtn=(Button)this.findViewById(R.id.query); deleteBtn=(Button)this.findViewById(R.id.delete); //注册监听器 createDataBaseBtn.setOnClickListener(new CreateDBListener()); updateDataBaseBtn.setOnClickListener(new UpdateDBListener()); insertBtn.setOnClickListener(new InsertListener()); updateBtn.setOnClickListener(new UpdateListener()); queryBtn.setOnClickListener(new QueryListener()); deleteBtn.setOnClickListener(new DeleteListener()); } class CreateDBListener implements OnClickListener{ @Override public void onClick(View v) { dbHelp=new DBHelp(SQLiteDemo.this,"linys",null,1); dbHelp.getWritableDatabase(); }} class UpdateDBListener implements OnClickListener{ @Override public void onClick(View v) { dbHelp=new DBHelp(SQLiteDemo.this,"linys",null,2); dbHelp.getWritableDatabase(); }} class InsertListener implements OnClickListener{ @Override public void onClick(View v) { //准备数据 ContentValues content=new ContentValues(); content.put("id", "12"); content.put("name", "linys"); //操作数据库 dbHelp=new DBHelp(SQLiteDemo.this,"linys",null,2); SQLiteDatabase db=dbHelp.getWritableDatabase(); db.insert("user", null, content); }} class DeleteListener implements OnClickListener{ @Override public void onClick(View v) { //操作数据库 dbHelp=new DBHelp(SQLiteDemo.this,"linys",null,2); SQLiteDatabase db=dbHelp.getWritableDatabase(); db.delete("user","id=?",new String[]{"12"}); }} class UpdateListener implements OnClickListener{ @Override public void onClick(View v) { //准备数据 ContentValues content=new ContentValues(); content.put("id", "12"); content.put("name", "linyisong"); //操作数据库 dbHelp=new DBHelp(SQLiteDemo.this,"linys",null,2); SQLiteDatabase db=dbHelp.getWritableDatabase(); db.update("user", content, "id=?" ,new String[]{"12"}); }} class QueryListener implements OnClickListener{ @Override public void onClick(View v) { dbHelp=new DBHelp(SQLiteDemo.this,"linys",null,2); SQLiteDatabase db=dbHelp.getReadableDatabase(); //Cursor游标相当于java中的Iterator Cursor cursor=db.query("user", new String[]{"id","name"}, "id=?",new String[]{"12"}, null,null, null); while(cursor.moveToNext()){ String id=cursor.getString(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); System.out.println("id: "+id+" name:"+name); } }} }
四、补充:调试adb
1.adb:android debug bridge
2.android自带的调试工具,可以进入内核,进行操作:adb shell 进入内核
3.编写的程序放在data/data对于的包(即我们在new出Program时指定的package)
4.如果程序有用到数据库,则数据库在database下
5.对数据进行操作前,先切换的sqlite3模式下:sqlite3 数据库名
6.标准的sql语句也用,另外.schema可以查看数据库中创建的表格语句