数据库类
package com.alleged.DB;
import com.alleged.bean.Restaurants;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class OperateDB extends SQLiteOpenHelper {
public OperateDB(Context context) {
super(context, "Db.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(10), address varchar(10), type varchar(10), notes varchar(10), phone varchar(10));");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if(oldVersion==1 && newVersion == 2){
db.execSQL("ALTER TABLE restaurants ADD phone TEXT;");
}
}
public void add(SQLiteDatabase db,Restaurants res){
db.execSQL("insert into restaurants(name,address,type,notes,phone) values (?,?,?,?,?); ", new Object[]{res.name,res.adress
,res.type,res.notes,res.phone});
System.out.println("add ok");
db.close();
}
public void del(SQLiteDatabase db,Restaurants res){
db.execSQL("delete from restaurants where name=?;",new Object[]{res.name});
System.out.println("del ok");
db.close();
}
public void updata(SQLiteDatabase db,Restaurants res){
db.execSQL("update restaurants set phone=? where name=?", new Object[]{res.phone,res.name});
System.out.println("updata ok");
db.close();
}
public Cursor resquery(SQLiteDatabase db,Restaurants res){
Cursor cursor =db.rawQuery("select * from restaurants",null);
System.out.println("resquery ok");
db.close();
return cursor;
}
}
主方法
package com.alleged.DB;
import com.alleged.bean.Restaurants;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Context mcontext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext = this;
findViewById(R.id.add).setOnClickListener(this);
findViewById(R.id.del).setOnClickListener(this);
findViewById(R.id.update).setOnClickListener(this);
findViewById(R.id.resquery).setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Restaurants r = new Restaurants();
switch(v.getId()){
case R.id.add:
OperateDB myDB = new OperateDB(mcontext);
SQLiteDatabase db = myDB.getReadableDatabase();
r.name = "中国红";
r.adress = "北京";
r.type = "中餐";
r.notes = "糖醋排骨";
r.phone = "135777777";
myDB.add(db, r);
db.close();
myDB.close();
break;
case R.id.del:
OperateDB myDB1 = new OperateDB(mcontext);
SQLiteDatabase db1 = myDB1.getReadableDatabase();
myDB1.del(db1, r);
db1.close();
myDB1.close();
break;
case R.id.update:
OperateDB myDB2 = new OperateDB(mcontext);
SQLiteDatabase db2 = myDB2.getReadableDatabase();
r.name = "中国红";
r.adress = "北京";
r.type = "中餐";
r.notes = "糖醋排骨";
r.phone = "88888888";
myDB2.updata(db2, r);
db2.close();
myDB2.close();
break;
case R.id.resquery:
OperateDB myDB3 = new OperateDB(mcontext);
SQLiteDatabase db3 = myDB3.getReadableDatabase();
myDB3.resquery(db3, r);
db3.close();
myDB3.close();
break;
}
}
}
界面
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.alleged.DB.MainActivity" >
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"
/>
<Button
android:id="@+id/del"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/del"
/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/update"
/>
<Button
android:id="@+id/resquery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/resquery"
/>
<TextView
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>