当前位置:文档之家› SQLITE最基本操作

SQLITE最基本操作

SQLite数据库的基本操作main.xml(主布局)<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="5dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名"/><EditTextandroid:id="@+id/edName"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="5dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="年龄"/><EditTextandroid:id="@+id/edAge"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="5dp"android:gravity="center"><Buttonandroid:id="@+id/btnInsert"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="添加"/><Buttonandroid:id="@+id/btnDelete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="删除"/><Buttonandroid:id="@+id/btnUpdate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="修改"/><Buttonandroid:id="@+id/btnSelect"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查询"/></LinearLayout><ListViewandroid:id="@+id/listView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="5dp"/></LinearLayout>listitem.xml(行布局)<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:padding="5dp"><TextViewandroid:id="@+id/id"android:layout_width="80dp"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/name"android:layout_width="80dp"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/age"android:layout_width="80dp"android:layout_height="wrap_content"/></LinearLayout>DBHelper.java(辅助类)package spl.db.sqlite;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DBHelper extends SQLiteOpenHelper{//创建构造器public DBHelper(Context context){super(context,"contactinfo.db",null,1);//contactinfo.db-数据库名,null-游标工厂,1-版本...下课休息...}@Overridepublic void onCreate(SQLiteDatabase db){//建表db.execSQL("create table person(_id integer primary key autoincrement,name varchar,age integer)");}@Overridepublic void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ //版本升级}}DBDemoActivity.java(主界面)package spl.db.sqlite;import java.util.ArrayList;import java.util.HashMap;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.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleAdapter;public class DBDemoActivity extends Activity{/**Called when the activity is first created.*///声明控件对象EditText edName,edAge;Button btnInsert,btnDelete,btnUpdate,btnSelect;ListView listView;@Overridepublic void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);setContentView(yout.main);//Fragment//初始化控件对象edName=(EditText)findViewById(R.id.edName);edAge=(EditText)findViewById(R.id.edAge);btnInsert=(Button)findViewById(R.id.btnInsert);btnDelete=(Button)findViewById(R.id.btnDelete);btnUpdate=(Button)findViewById(R.id.btnUpdate);btnSelect=(Button)findViewById(R.id.btnSelect);listView=(ListView)findViewById(R.id.listView);//给按钮添加监听事件btnInsert.setOnClickListener(btnListener);btnDelete.setOnClickListener(btnListener);btnUpdate.setOnClickListener(btnListener);btnSelect.setOnClickListener(btnListener);selectAll();//全查询}private OnClickListener btnListener=new OnClickListener(){@Overridepublic void onClick(View v){//判断是哪一个按钮被点击switch(v.getId()){case R.id.btnInsert:insert();selectAll();break;case R.id.btnDelete:delete();selectAll();break;case R.id.btnUpdate:update();selectAll();break;case R.id.btnSelect:select();break;default:break;}}};//增protected void insert(){DBHelper helper=new DBHelper(this);SQLiteDatabase db=helper.getReadableDatabase();ContentValues values=new ContentValues();//HashMapString name=edName.getText().toString().trim();String age=edAge.getText().toString().trim();values.put("name",name);values.put("age",age);db.insert("person",null,values);//"person"-表名,null-空值列,valuesdb.close();//关闭连接}//删protected void delete(){DBHelper helper=new DBHelper(this);SQLiteDatabase db=helper.getReadableDatabase();String name=edName.getText().toString();int row=db.delete("person","name=?",new String[]{name});//"person"-表名,"name=?"-where模板,new String[]{name}-占位符的值Log.i("spl","delete row="+row);db.close();}//改protected void update(){DBHelper helper=new DBHelper(this);SQLiteDatabase db=helper.getReadableDatabase();ContentValues values=new ContentValues();String name=edName.getText().toString();String age=edAge.getText().toString();values.put("age",age);int row=db.update("person",values,"name=?",//where模板new String[]{name});//-占位符的值Log.i("spl","update row="+row);db.close();//关闭连接}//查询protected void select(){ArrayList<HashMap<String,Object>>list=newArrayList<HashMap<String,Object>>();DBHelper helper=new DBHelper(this);SQLiteDatabase db=helper.getReadableDatabase();String age=edAge.getText().toString();Cursor cursor=db.query("person",//表名new String[]{"_id","name","age"},//列名"age=?"//where模板,new String[]{age},//-占位符的值null,//分组null,//havingnull);//排序"age desc"//解析结果集while(cursor.moveToNext()){//移到下一行HashMap<String,Object>map=new HashMap<String,Object>();map.put("id",cursor.getString(0));map.put("name",cursor.getString(1));map.put("age",cursor.getString(2));list.add(map);}cursor.close();db.close();//UI更新listView.setAdapter(new SimpleAdapter(this,list,yout.listitem,new String[]{"id","name","age"},new int[]{R.id.id,,R.id.age}));}//全查询protected void selectAll(){ArrayList<HashMap<String,Object>>list=newArrayList<HashMap<String,Object>>();DBHelper helper=new DBHelper(this);SQLiteDatabase db=helper.getReadableDatabase();Cursor cursor=db.rawQuery("select*from person",null);while(cursor.moveToNext()){HashMap<String,Object>map=new HashMap<String,Object>();map.put("id",cursor.getString(0));map.put("name",cursor.getString(1));map.put("age",cursor.getString(2));list.add(map);}cursor.close();db.close();listView.setAdapter(new SimpleAdapter(this,list,yout.listitem,new String[]{"id","name","age"},new int[]{R.id.id,,R.id.age}));}}。

相关主题