例如:
SQLiteDatabase dbWrite = db.getWritableDatabase();
SQLiteDatabase详细的介绍可参照谷歌方法API: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
三:案例展示:
创建一个DB 类,继承 SQLiteOpenHelper,factory制定为空,版本指定为1,代码如下:
(注意:SQLite数据库需要为表创建一个主键,且规定主键的形式为“_id”命名)
1 package activity.cyq.sqlitelearn; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 7 8 public class DB extends SQLiteOpenHelper { 9 public DB(Context context) { 10 super(context, "db", null, 1); 11 /*db:数据库名称 null:操作数据库工厂 1:数据库版本号*/ 12 } 13 14 @Override 15 public void onCreate(SQLiteDatabase db) { 16 db.execSQL("Create Table user( _id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT DEFAULT NONE , password TEXT DEFAULT NONE) "); 17 } 18 19 @Override 20 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 21 22 } 23 }
案例页面布局如下,通过添加数据按钮实现将用户名和密码添加如数据库中。
布局XML文件如下 :activity_main.xml itemview.xml
activity_main.xml如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity"> 11 12 <LinearLayout 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:orientation="horizontal"> 16 17 <TextView 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:text="用户名:" /> 22 23 <EditText 24 android:id="@+id/usernameEdit" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_weight="6" /> 28 29 <TextView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_weight="1" 33 android:text="密码:" /> 34 35 <EditText 36 android:id="@+id/passwordEdit" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:layout_weight="6" /> 40 41 <Button 42 android:id="@+id/addDataBtn" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_weight="1" 46 android:text="添加数据" /> 47 48 </LinearLayout> 49 50 <RelativeLayout 51 android:layout_width="match_parent" 52 android:layout_height="wrap_content"> 53 54 <LinearLayout 55 android:layout_width="match_parent" 56 android:layout_height="wrap_content" 57 android:layout_centerInParent="true"> 58 59 <TextView 60 android:layout_width="match_parent" 61 android:layout_height="wrap_content" 62 android:layout_centerInParent="true" 63 android:text="数据展示" 64 android:textSize="20dp" /> 65 </LinearLayout> 66 </RelativeLayout> 67 68 <LinearLayout 69 android:layout_width="match_parent" 70 android:layout_height="wrap_content"> 71 72 <TextView 73 android:layout_width="wrap_content" 74 android:layout_height="wrap_content" 75 android:layout_weight="1" 76 android:text="用户名" 77 android:textSize="20dp" /> 78 79 <TextView 80 android:layout_width="wrap_content" 81 android:layout_height="wrap_content" 82 android:layout_weight="1" 83 android:text="密 码" 84 android:textSize="20dp" /> 85 </LinearLayout> 86 87 <ListView 88 android:id="@android:id/list" 89 android:layout_width="match_parent" 90 android:layout_height="wrap_content" 91 android:layout_weight="10" 92 android:background="@color/material_blue_grey_800"></ListView> 93 94 95 </LinearLayout>
itemview.xml:如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 7 <TextView 8 android:id="@+id/usernameItem" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_weight="1" 12 android:text="username" 13 android:textSize="20dp" /> 14 15 <TextView 16 android:id="@+id/passwordItem" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_weight="1" 20 android:text="password" 21 android:textSize="20dp" /> 22 23 24 </LinearLayout>
MainActivity中代码如下:
1 package activity.cyq.sqlitelearn; 2 3 4 import android.app.AlertDialog; 5 import android.app.ListActivity; 6 import android.content.ContentValues; 7 import android.content.DialogInterface; 8 import android.database.Cursor; 9 import android.database.sqlite.SQLiteDatabase; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.widget.AdapterView; 13 import android.widget.Button; 14 import android.widget.EditText; 15 import android.widget.SimpleCursorAdapter; 16 17 public class MainActivity extends ListActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener { 18 19 private EditText username; 20 private EditText password; 21 private Button addData; 22 private SQLiteDatabase dbWrite, dbRead; 23 private SimpleCursorAdapter adapter; 24 private DB db; 25 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 32 username = (EditText) findViewById(R.id.usernameEdit); 33 password = (EditText) findViewById(R.id.passwordEdit); 34 addData = (Button) findViewById(R.id.addDataBtn); 35 addData.setOnClickListener(this); 36 /*创建数据库*/ 37 db = new DB(MainActivity.this); 38 dbWrite = db.getWritableDatabase(); 39 dbRead = db.getReadableDatabase(); 40 41 adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.itemview, null, new String[]{"username", "password"}, 42 new int[]{R.id.usernameItem, R.id.passwordItem}); 43 setListAdapter(adapter); 44 refresh(); 45 46 getListView().setOnItemLongClickListener(this); 47 48 } 49 50 /*刷新ListView数据*/ 51 public void refresh() { 52 Cursor cursor = dbRead.query("user", null, null, null, null, null, null, null); 53 adapter.changeCursor(cursor); 54 } 55 56 /*添加数据点击事件*/ 57 @Override 58 public void onClick(View v) { 59 String usernameData = username.getText().toString(); 60 String passwordData = password.getText().toString(); 61 ContentValues values = new ContentValues(); 62 values.put("username", usernameData); 63 values.put("password", passwordData); 64 dbWrite.insert("user", null, values); 65 values.clear(); 66 67 refresh(); 68 } 69 70 /*编辑item按钮的点击事件*/ 71 72 73 /*ListViewItem的长按事件处理*/ 74 @Override 75 public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { 76 77 new AlertDialog.Builder(MainActivity.this).setTitle("警告").setMessage("是否删除该数据").setIcon(R.drawable.delete) 78 .setPositiveButton("是", new DialogInterface.OnClickListener() { 79 @Override 80 public void onClick(DialogInterface dialog, int which) { 81 Cursor c = adapter.getCursor(); 82 // c.moveToPosition(position);/*????*/ 83 int item_id = c.getInt(c.getColumnIndex("_id")); 84 dbWrite.delete("user", "_id=?", new String[]{item_id + ""}); 85 refresh(); 86 } 87 }).setNegativeButton("否", null) 88 .show(); 89 90 91 return true; 92 } 93 }