[Android开发视频教学].01_16_SQLite使用方法
学习使用SQLiteOpenHelper,SQLDataBase,ContentValues
感觉SQLiteOpenHelper不好用,有许多操作感觉很繁琐,不过还是看懂了,并且做了实现
注:生成数据库操作只能生成一次,没有找到删除数据库的方法,这里写的代码如果执行是会报错的,所以没有将此事件绑定到监听器上
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="false" android:layout_centerVertical="false" android:text="@string/hello_world" tools:context=".Activity13" /> <Button android:id="@+id/createDB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:text="@string/createDB" /> <Button android:id="@+id/upgradeDB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/createDB" android:text="@string/upgradeDB" /> <Button android:id="@+id/deleteDB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/upgradeDB" android:text="@string/deleteDB" /> <Button android:id="@+id/insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/deleteDB" android:text="@string/insert" /> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/insert" android:text="@string/update" /> <Button android:id="@+id/query" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/update" android:text="@string/query" /> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/query" android:text="@string/delete" /> </RelativeLayout>
<resources> <string name="app_name">SQLite操作</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_activity13">SQLite操作</string> <string name="createDB">创建数据库</string> <string name="upgradeDB">升级数据库</string> <string name="deleteDB">删除数据库</string> <string name="insert">添加数据</string> <string name="update">更新数据</string> <string name="query">查询数据</string> <string name="delete">删除数据</string> </resources>
package fengda.android13; import java.io.File; import android.annotation.SuppressLint; 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; public class Activity13 extends Activity { private Button createDB; private Button upgradeDB; private Button deleteDB; private Button insert; private Button update; private Button query; private Button delete; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity13); createDB = (Button) findViewById(R.id.createDB); createDB.setOnClickListener(new CreateDBClickListenen()); upgradeDB = (Button) findViewById(R.id.upgradeDB); upgradeDB.setOnClickListener(new UpgradeDBClickListener()); deleteDB = (Button) findViewById(R.id.deleteDB); //deleteDB.setOnClickListener( new DeleteDBClickListener()); insert = (Button) findViewById(R.id.insert); insert.setOnClickListener(new InsertClickListener()); update = (Button) findViewById(R.id.update); update.setOnClickListener(new UpdateClickListener()); query = (Button) findViewById(R.id.query); query.setOnClickListener(new QueryClickListener()); delete = (Button) findViewById(R.id.delete); delete.setOnClickListener(new DeleteClickListener()); } //创建数据库 class CreateDBClickListenen implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13"); myDBHelper.getReadableDatabase(); } } //更新数据库版本 class UpgradeDBClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13",2); myDBHelper.getReadableDatabase(); } } //删除数据库 class DeleteDBClickListener implements OnClickListener { @SuppressLint("NewApi") @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13"); SQLiteDatabase db = myDBHelper.getWritableDatabase(); db.deleteDatabase(new File("android13")); } } //添加一条数据信息 class InsertClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13"); SQLiteDatabase db = myDBHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("id", 1); values.put("name", "牛牛"); db.insert("person", null, values); } } //更新数据 class UpdateClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13"); SQLiteDatabase db = myDBHelper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name", "niuniu"); db.update("person", values, "id=?", new String[]{"1"}); } } //查询数据 class QueryClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13"); SQLiteDatabase db = myDBHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[]{"id","name"}, "id=?", new String[]{"1"} , null, null, null); while(cursor!=null && cursor.moveToNext()){ int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); System.out.println("id="+id+", name="+name); } } } //删除数据 class DeleteClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub MyDBHelper myDBHelper = new MyDBHelper(Activity13.this,"android13"); SQLiteDatabase db = myDBHelper.getWritableDatabase(); db.delete("person", "id=?", new String[]{"1"}); } } }
package fengda.android13; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MyDBHelper extends SQLiteOpenHelper { public static final int VERSION=1; public MyDBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public MyDBHelper(Context context,String name,int version){ this(context,name,null,version); } public MyDBHelper(Context context,String name){ this(context,name,VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("创建一个数据库"); db.execSQL("create table person(id int, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub System.out.println("升级数据库"); } }
Android调试起来不方便,下一讲正好讲调试操作,哈哈。