1、 创建DBhelper类 来实现数库的创建
package net.android.androidDB;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBhelper extends SQLiteOpenHelper{
private static final int VERSION = 1;
public DBhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
public DBhelper(Context context, String name) {
this(context, name, VERSION);
}
public DBhelper(Context context, String name, int version) {
this(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
System.out.println("create a DB");
// arg0.execSQL("create table user(id int, name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
System.out.println("update a DB");
}
}
2、 创建运行类
package net.android.android_db;
import java.util.Date;
import java.util.Random;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.ko8e.DBhelper;
public class android_db extends Activity {
/** Called when the activity is first created. */
private Button button1 = null;
private Button button2 = null;
private Button button3 = null;
private Button button4 = null;
private Button button5 = null;
private TextView show=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show=(TextView)findViewById(R.id.show);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button1.setOnClickListener(new CreateListener());
button2.setOnClickListener(new UpdateRecordListener());
button3.setOnClickListener(new InsertListener());
button4.setOnClickListener(new QueryListener());
button5.setOnClickListener(new UpdateListener());
}
private class CreateListener implements OnClickListener {
@Override
public void onClick(View v) {
DBhelper db = new DBhelper(android_db.this, "ko8e_db");
SQLiteDatabase sqld = db.getReadableDatabase();
}
}
private class UpdateRecordListener implements OnClickListener {
@Override
public void onClick(View v) {
DBhelper db = new DBhelper(android_db.this, "ko8e_db");
SQLiteDatabase sqld = db.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", "kobe bryant");
sqld.update("user", values, "id=?", new String[]{"1"});
}
}
private class InsertListener implements OnClickListener {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("id", 1);
Random ran=new Random();
values.put("name", new Date().toString()+" "+String.valueOf(ran.nextInt(10000))+" ko8e");
DBhelper db = new DBhelper(android_db.this, "ko8e_db", Context.MODE_WORLD_WRITEABLE);
SQLiteDatabase sqld = db.getReadableDatabase();
sqld.insert("user", null, values);
}
}
private class QueryListener implements OnClickListener {
@Override
public void onClick(View v) {
DBhelper db = new DBhelper(android_db.this, "ko8e_db");
SQLiteDatabase sqld = db.getReadableDatabase();
Cursor cursor = sqld.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
String resu="";
while(cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
//System.out.println("quer: " + name);
Log.e("****query", name);
resu+=" name:"+cursor.getString(cursor.getColumnIndex("name"))+" aa /r/n";
}
Log.e("**result*", resu);
// show.setText("dddd");
}
}
private class UpdateListener implements OnClickListener {
@Override
public void onClick(View v) {
DBhelper db = new DBhelper(android_db.this, "ko8e_db", 2);
SQLiteDatabase sqld = db.getReadableDatabase();
}
}
}
3、 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btn_create" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="创建表(含库)" />
<Button android:id="@+id/btn_insert" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="添加记录" />
<Button android:id="@+id/btn_query" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="查询记录" />
<Button android:id="@+id/btn_update" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="更新记录" />
<Button android:id="@+id/btn_delete" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="删除记录" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="ID" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/edID" android:layout_width="100px"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView android:id="@+id/show" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="show"/>
</LinearLayout>