在Android中存儲數據能夠用文件、數據庫、網絡,其中文件和數據庫是最經常使用的,數據庫咱們經常使用的就是Sqlite,它是一種經量級的、嵌入式的關係型數據庫;在android中當須要操做SQLite數據庫的時候須要獲得一個SQLiteOpenHelper對象,而SQLiteOpenHelper是一個抽象類,用戶須要繼承這個類html
package com.example.sqlite.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * DatabaseHelper做爲一個訪問SQLite的助手類,提供兩個方面的功能, * 第一,getReadableDatabase(),getWritableDatabase()能夠得到SQLiteDatabse對象,經過該對象能夠對數據庫進行操做 * 第二,提供了onCreate()和onUpgrade()兩個回調函數,容許咱們在建立和升級數據庫時,進行本身的操做 * @author Administrator * */ public class DatabaseHelper extends SQLiteOpenHelper{ public static final int VERSION = 1; public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public DatabaseHelper(Context context, String name){ this(context, name, VERSION); Log.i("debug", "DatabaseHelper"); } public DatabaseHelper(Context context, String name, int version){ this(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub Log.i("debug", "create a database"); //執行SQL語句 db.execSQL("create table user (id int, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.i("debug", "Upgrade a database"); } }
下面是對數據庫的進行增、刪、改、查操做java
佈局文件android
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/create_db" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/create_db"/> <Button android:id="@+id/update_db" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/update_db"/> <Button android:id="@+id/insert" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/insert"/> <Button android:id="@+id/update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/update"/> <Button android:id="@+id/query" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/query"/> <Button android:id="@+id/delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/delete"/> </LinearLayout>
在Activity處理代碼sql
package com.example.sqlite; import com.example.sqlite.db.DatabaseHelper; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * SQLite的建立、更新、修改、插入、查詢、刪除操做 * @author Administrator * */ public class SqliteActivity extends Activity { private Button create_db; private Button update_db; //更新數據庫的版本號 private Button insert; private Button update; private Button query; private Button delete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sqlite); create_db = (Button)findViewById(R.id.create_db); update_db = (Button)findViewById(R.id.update_db); insert = (Button)findViewById(R.id.insert); update = (Button)findViewById(R.id.update); query = (Button)findViewById(R.id.query); delete = (Button)findViewById(R.id.delete); create_db.setOnClickListener(new CreateListener()); update_db.setOnClickListener(new UpdateDBListener()); insert.setOnClickListener(new InsertListener()); update.setOnClickListener(new UpdateListener()); query.setOnClickListener(new QueryListener()); delete.setOnClickListener(new Deletelistener()); } class CreateListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Log.i("debug", "on create db"); DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db"); //只有調用了DatabaseHelper的getReadableDatabase()或getWritableDatabase()纔會建立數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); } } /** * 更新數據庫的版本號 * @author Administrator * */ class UpdateDBListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Log.i("debug", "on update db"); DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db", 2); SQLiteDatabase db = dbHelper.getReadableDatabase(); } } class InsertListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("debug", "insert db"); ContentValues values = new ContentValues(); //向該對象插入鍵值對,其中鍵是列名,值是但願插入這一列的值,值的數據類型必須一致 values.put("id", 1); values.put("name", "zhang"); DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db"); SQLiteDatabase db = dbHelper.getWritableDatabase(); //調用insert方法插入數據 db.insert("user", null, values); } } class UpdateListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("debug", "update db"); DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db"); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", "zhangsanfeng"); //第一個參數是要更新的表名 //第二個參數是一個ContentValeus對象 //第三個參數是where子句 db.update("user", values, "id=?", new String[]{"1"}); } } class QueryListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("debug", "query db"); DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db"); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null); int count = cursor.getCount(); System.out.println("query---count = >" + count); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); System.out.println("query--->" + name); } } } class Deletelistener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Log.i("debug", "delete db"); DatabaseHelper dbHelper = new DatabaseHelper(SqliteActivity.this, "text_mars_db"); SQLiteDatabase db = dbHelper.getWritableDatabase(); db.delete("user", "id=?", new String[]{"1"}); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_sqlite, menu); return true; } }
運行效果數據庫
樣例代碼網絡
點擊打開連接
app