//首先创建数据库
package com.bawei.mysqliteopenhelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context) {
super(context, "007.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
arg0.execSQL("create table person(_id integer primary key autoincrement,name varchar(10),age integer,sex varchar(2),address varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
//然后创建Dao类
package com.bawei.dao;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import
com.bawei.mysqliteopenhelper.MySQLiteOpenHelper;
public class Dao {
private MySQLiteOpenHelper sq;
public Dao(Context context){
sq = new MySQLiteOpenHelper(context);
}
//添加数据
public void insert(){
SQLiteDatabase db=sq.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name", "凯皇");
values.put("age", 32);
values.put("sex", "男");
values.put("address", "火之国");
db.insert("person", null, values);
db.close();
}
//删除数据
public void delete(){
SQLiteDatabase db=sq.getWritableDatabase();
db.delete("person", "name=?",new String[]{"
凯皇"} );
db.close();
}
//修改数据
public void update(){
SQLiteDatabase db=sq.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name", "晓");
values.put("age", 25);
values.put("sex", "女");
values.put("address", "无国");
db.update("person", values, "name=?",new
String[]{"凯皇"});
db.close();
}
//查询数据
public List<Object> query(){
SQLiteDatabase db=sq.getWritableDatabase();
Cursor cursor=db.query("person", null, null,
null, null, null, null,null);
List<Object> list=null;
while(cursor.moveToNext()){
list=new ArrayList<Object>();
String name=cursor.getString
(cursor.getColumnIndexOrThrow("name"));
int age =cursor.getInt
(cursor.getColumnIndexOrThrow("age"));
String sex=cursor.getString
(cursor.getColumnIndexOrThrow("sex"));
String address=cursor.getString
(cursor.getColumnIndexOrThrow("address"));
list.add(name);
list.add(age);
list.add(sex);
list.add(address);
}
return list;
}
}
//然后 是在Activity中的操作
package com.bawei.activity;
import java.util.List;
import com.bawei.dao.Dao;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dao = new Dao(this);
//获取控件
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv4 = (TextView) findViewById(R.id.tv4);
}
public void insert(View view){
dao.insert();
}
public void delete(View view){
dao.delete();
}
public void update(View view){
dao.update();
}
public void query(View view){
List<Object> list=dao.query();
if(list==null){
Toast.makeText(this, "您数据库中暂时没有数据呢!", 0).show();
tv1.setText("");
tv2.setText("");
tv3.setText("");
tv4.setText("");
}else{
tv1.setText((CharSequence) list.get(0));
tv2.setText(list.get(1)+"");
tv3.setText((CharSequence) list.get(2));
tv4.setText((CharSequence) list.get(3));
}
}
}