activity12.java
- package com.wansha;
-
- import Android.app.Activity;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- import com.wansha.db.DatabaseHelper;
-
- public class Activity12 extends Activity {
- private Button createDatabase;
- private Button updateDatabase;
- private Button insert;
- private Button update;
- private Button read;
- private Button delete;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.createDatabase = (Button)this.findViewById(R.id.createDatabase);
- this.createDatabase.setOnClickListener(new CreateDatabaseListener());
-
- this.updateDatabase = (Button)this.findViewById(R.id.updateDatabase);
- this.updateDatabase.setOnClickListener(new UpdateDatabaseListener());
-
- this.insert = (Button)this.findViewById(R.id.insert);
- this.insert.setOnClickListener(new InsertListener());
-
- this.update = (Button)this.findViewById(R.id.update);
- this.update.setOnClickListener(new UpdateListener());
-
- this.read = (Button)this.findViewById(R.id.read);
- this.read.setOnClickListener(new ReadListener());
-
- this.delete = (Button)this.findViewById(R.id.delete);
- this.delete.setOnClickListener(new DeleteListener());
-
- }
- class CreateDatabaseListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp");
- sqlite.getReadableDatabase();
- }
- }
- class UpdateDatabaseListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- System.out.println("UpdateDatabaseListener is invoked !!!");
- SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp",2);
- sqlite.getReadableDatabase();
- }
- }
- class InsertListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp");
- SQLiteDatabase database = sqlite.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("username", "xiaopeng");
- database.insert("sharp", null, values);
- }
- }
- class UpdateListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp");
- SQLiteDatabase database = sqlite.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("username", "pengpeng");
- database.update("sharp", values, "id=?", new String[]{"2"});
- }
- }
- class ReadListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp");
- SQLiteDatabase database = sqlite.getWritableDatabase();
- Cursor cursor = database.query("sharp", new String[]{"id","username"}, null, null, null, null, null);
- while(cursor.moveToNext()){
- String username = cursor.getString(cursor.getColumnIndex("username"));
- System.out.println("username---->" + username);
- }
- }
- }
- class DeleteListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- SQLiteOpenHelper sqlite = new DatabaseHelper(Activity12.this,"sharp");
- SQLiteDatabase database = sqlite.getWritableDatabase();
- database.delete("sharp", "id=?", new String[]{"1"});
- }
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <Button android:id="@+id/createDatabase"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="創建數據庫"
- />
- <Button android:id="@+id/updateDatabase"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="更新數據庫"
- />
- <Button android:id="@+id/insert"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="插入數據"
- />
- <Button android:id="@+id/update"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="更新數據"
- />
- <Button android:id="@+id/read"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="讀取數據"
- />
- <Button android:id="@+id/delete"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="刪除數據"
- />
- </LinearLayout>
db.execSQL("create table sharp(id integer primary key autoincrement, username varchar(255))");
創建自增長ID
DatebaseHelper.java
- package com.wansha.db;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteOpenHelper;
-
- public class DatabaseHelper extends SQLiteOpenHelper{
- private static final int VERSION = 1;
- public DatabaseHelper(Context context, String name, CursorFactory factory,
- int version) {
- super(context, name, factory, version);
- }
- public DatabaseHelper(Context context, String name,int version) {
- this(context, name, null, version);
- }
- public DatabaseHelper(Context context, String name) {
- this(context, name, VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- System.out.println("create table is invoked !!!");
- db.execSQL("create table sharp(id integer primary key autoincrement, username varchar(255))");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- System.out.println("update table is invoked !!!");
- }
-
- }