<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/buttonSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Save" /> <Button android:id="@+id/buttonLoad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/buttonSave" android:layout_alignBottom="@+id/buttonSave" android:layout_centerHorizontal="true" android:text="Load" /> </RelativeLayout>
DBHelper:
package com.cy.mysqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public static final int dbVersion = 1; public static final String dbName = "test.db"; public DBHelper(Context context) { super(context, dbName, null, dbVersion); } @Override public void onCreate(SQLiteDatabase db) { final String sql = "CREATE TABLE mytest(number, name);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { final String sql = "DROP TABLE IF EXISTS mytest;"; db.execSQL(sql); onCreate(db); } }
MainActivity
package com.cy.mysqlite; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button button_Save; Button button_Load; DBHelper myDB; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private final void init() { button_Save = (Button) findViewById(R.id.buttonSave); button_Load = (Button) findViewById(R.id.buttonLoad); button_Save.setOnClickListener(SaveclickListener); button_Load.setOnClickListener(LoadclickListener); myDB = new DBHelper(this); } private final Button.OnClickListener SaveclickListener = new Button.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = myDB.getWritableDatabase(); ContentValues values = new ContentValues(); for (int index = 1; index <= 5; index++) { values.put("number", index); values.put("name", "test" + index); db.insert("mytest", null, values); } Log.i("db", "done!"); db.close(); } }; private final Button.OnClickListener LoadclickListener = new Button.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = myDB.getReadableDatabase(); String[] columns = new String[] { "number", "name" }; Cursor cursor = db.query("mytest", columns, null, null, null, null, null); while (cursor.moveToNext()) { Log.i("db number", cursor.getInt(0) + ""); Log.i("db name", cursor.getString(1)); } cursor.close(); db.close(); } }; }
建立完成:
查詢結果:
參考資料:
http://gisanfu.pixnet.net/blog/post/7941810
http://blog.tonycube.com/2011/11/androidsqlite.html