android SQLiteDatebase 实践

来源:互联网 发布:比尔克软件 编辑:程序博客网 时间:2024/06/10 03:29
 android 平台下建立的一个数据库小实例。实现了数据的增、删、改、通过对话框的形式去修改、删除数据

MdbHelper类:

view plain
  1. package training.android.com;  
  2.   
  3. import android.content.ContentValues;  
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.SQLException;  
  7. import android.database.sqlite.SQLiteDatabase;  
  8. import android.util.Log;  
  9.   
  10. public class MdbHelper {  
  11.     private SQLiteDatabase mdb;  
  12.     private sqldataHelper mdbhelp;  
  13.     private final Context cxt;  
  14.     public static final String KeyRowID = "_id";  
  15.     public static final String KeyRowname = "StudentName";  
  16.     public static final String KeyRowAge = "StudentAge";  
  17.     public static final String KeyRowClass = "StudentClass";  
  18.     public static final String KeyRowSchool = "StudentSchool";  
  19.     private static final String TableName = "Student";//  
  20.   
  21.     public MdbHelper(Context ct) {  
  22.         this.cxt = ct;  
  23.     }  
  24.   
  25.     public MdbHelper open() throws SQLException {  
  26.         mdbhelp = new sqldataHelper(cxt, 1);  
  27.         setMdb(mdbhelp.getWritableDatabase());  
  28.         return this;  
  29.   
  30.     }  
  31.   
  32.     public void close() {  
  33.         mdbhelp.close();  
  34.     }  
  35.   
  36.     public void InsertStudent(int studnetid, String studentname,  
  37.             String studentage, String studentclass, String studentschool) {  
  38.         ContentValues values = new ContentValues();  
  39.         values.put(KeyRowID, studnetid);  
  40.         values.put(KeyRowname, studentname);  
  41.         values.put(KeyRowAge, studentage);  
  42.         values.put(KeyRowClass, studentclass);  
  43.         values.put(KeyRowSchool, studentschool);  
  44.         mdb.insert(TableName, null, values);  
  45.     }  
  46.   
  47.     public void UpdateStudent(int studentid, String studentname,  
  48.             String studentage, String studentclass, String studentschool) {  
  49.         ContentValues values = new ContentValues();  
  50.         values.put(KeyRowname, studentname);  
  51.         values.put(KeyRowAge, studentage);  
  52.         values.put(KeyRowClass, studentclass);  
  53.         values.put(KeyRowSchool, studentschool);  
  54.         mdb.update(TableName, values, KeyRowID + "=" + studentid, null);  
  55.     }  
  56.   
  57.     public Cursor simpleStudent(long studentid) throws SQLException {  
  58.         return mdb.query(TableName, new String[] { KeyRowID, KeyRowname,  
  59.                 KeyRowAge, KeyRowClass, KeyRowSchool }, KeyRowID + "="  
  60.                 + studentid, nullnullnullnull);  
  61.         // return mdb.query(TableName, new String[]{KeyRowID,  
  62.         // KeyRowname,KeyRowAge,KeyRowClass,KeyRowSchool},  
  63.         // KeyRowID+"="+studentid, null, null, null, null);  
  64.         // return mdb.query(TableName, new String[]{KeyRowID,  
  65.         // KeyRowname,KeyRowAge,KeyRowClass,KeyRowSchool},  
  66.         // KeyRowID+"="+studentid, null, null, null, null);  
  67.     }  
  68.     public boolean deletestudent(long rowId) {  
  69.           return mdb.delete(TableName, KeyRowID + "=" + rowId, null) > 0;               //(4)  
  70.         }  
  71.     public Cursor fetchMyTableById(long rowId) throws SQLException {  
  72.         return mdb.query(TableName, new String[] { KeyRowID, KeyRowname,  
  73.                 KeyRowAge, KeyRowClass, KeyRowSchool }, KeyRowID + "=" + rowId,  
  74.                 nullnullnullnull);  
  75.     }  
  76.   
  77.     public Cursor QueryStudentALl() {  
  78.         return mdb.query(TableName, new String[] { KeyRowID, KeyRowname,  
  79.                 KeyRowAge, KeyRowClass, KeyRowSchool }, nullnullnullnull,  
  80.                 nullnull);  
  81.   
  82.     }  
  83.   
  84.     public void setMdb(SQLiteDatabase mdb) {  
  85.         this.mdb = mdb;  
  86.     }  
  87.   
  88.     public SQLiteDatabase getMdb() {  
  89.         return mdb;  
  90.     }  
  91.   
  92. }  

SQLiteOpenHelper继承:

view plain
  1. package training.android.com;  
  2.   
  3.   
  4. import android.content.Context;  
  5.   
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.database.sqlite.SQLiteOpenHelper;  
  8. public class sqldataHelper extends SQLiteOpenHelper {  
  9.     private static final String Databasename="SmsStudent";  
  10.     private static final String TableName = "Student";//  
  11.     public static final String KeyRowID = "_id";  
  12.     public static final String KeyRowname = "StudentName";  
  13.     public static final String KeyRowAge = "StudentAge";  
  14.     public static final String KeyRowClass = "StudentClass";  
  15.     public static final String KeyRowSchool = "StudentSchool";  
  16.     private static final String StudentTableSql = "CREATE TABLE IF NOT EXISTS "  
  17.             + TableName + "(" + KeyRowID + " INTEGER PRINAMARY KEY," + KeyRowname  
  18.             + " VARCHAR," + KeyRowAge + " VARCHAR," + KeyRowClass + " VARCHAR,"  
  19.             + KeyRowSchool + " VARCHAR);";  
  20.     private static final String UpdateTableGradSql = "DROP TABLE IF EXISTS"  
  21.             + TableName;  
  22.     public sqldataHelper(Context context, int version) {  
  23.         super(context, Databasename, null, version);  
  24.         // TODO Auto-generated constructor stub  
  25.     }  
  26.   
  27.     @Override  
  28.     public void onCreate(SQLiteDatabase db) {  
  29.         // TODO Auto-generated method stub  
  30.                 db.execSQL(StudentTableSql);  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  35.         // TODO Auto-generated method stub  
  36.         db.execSQL(UpdateTableGradSql);  
  37.         onCreate(db);  
  38.     }  
  39.       
  40. }  

页面代码:

view plain
  1. package training.android.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.content.DialogInterface.OnClickListener;  
  7. import android.database.Cursor;  
  8. import android.os.Bundle;  
  9. import android.view.LayoutInflater;  
  10. import android.view.Menu;  
  11. import android.view.MenuItem;  
  12. import android.view.View;  
  13. import android.widget.AdapterView;  
  14. import android.widget.EditText;  
  15. import android.widget.ListView;  
  16. import android.widget.SimpleCursorAdapter;  
  17. import android.widget.Toast;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19. //创建人:周昕  
  20. //创建时间:2010-4-1  
  21. public class studentdatabase extends Activity {  
  22.     private MdbHelper mdbhelper;  
  23.     public static final String KeyRowID = "_id";  
  24.     public static final String KeyRowname = "StudentName";  
  25.     public static final String KeyRowAge = "StudentAge";  
  26.     public static final String KeyRowClass = "StudentClass";  
  27.     public static final String KeyRowSchool = "StudentSchool";  
  28.     private EditText studentname;  
  29.     private EditText studentage;  
  30.     private EditText studentclass;  
  31.     private EditText studentschool;  
  32.     private String name;  
  33.     private String age;  
  34.     private String Class;  
  35.     private String School;  
  36.     private int Studentid;  
  37.     // private static final String TableName = "Student";//  
  38.     /** Called when the activity is first created. */  
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.main);  
  43.   
  44.         Binddata();  
  45.     }  
  46.   
  47.     public boolean onCreateOptionsMenu(Menu menu) {  
  48.         super.onCreateOptionsMenu(menu);  
  49.         menu.add("新增");  
  50.         return true;  
  51.     }  
  52.   
  53.     public boolean onOptionsItemSelected(MenuItem items) {  
  54.         super.onOptionsItemSelected(items);  
  55.         switch (items.getItemId()) {  
  56.         case 0:  
  57.             // Toast.makeText(this, "新增", Toast.LENGTH_SHORT).show();  
  58.             LayoutInflater factory = LayoutInflater.from(this);  
  59.             View myview = factory.inflate(R.layout.addstundet, null);  
  60.             studentname = (EditText) myview  
  61.                     .findViewById(R.id.insertstudentname);  
  62.             studentage = (EditText) myview.findViewById(R.id.insertstudentage);  
  63.             studentclass = (EditText) myview  
  64.                     .findViewById(R.id.insertstudentclass);  
  65.             studentschool = (EditText) myview  
  66.                     .findViewById(R.id.insertstudentschool);  
  67.             OnClickListener click1 = new OnClickListener() {  
  68.                 @Override  
  69.                 public void onClick(DialogInterface dialog, int which) {  
  70.                     // TODO Auto-generated method stub  
  71.                     String name = studentname.getText().toString();  
  72.                     String age = studentage.getText().toString();  
  73.                     String sclass = studentclass.getText().toString();  
  74.                     String school = studentschool.getText().toString();  
  75.                     mdbhelper.open();  
  76.                     Cursor firstcurss = mdbhelper.QueryStudentALl();  
  77.                     int max;  
  78.                     int temp = 0;  
  79.                     while (firstcurss.moveToNext()) {  
  80.   
  81.                         max = firstcurss.getInt(0);  
  82.                         if (temp < max)  
  83.                             temp = max;  
  84.                     }  
  85.                     int maxIntID = temp;  
  86.                     mdbhelper.InsertStudent(maxIntID + 1, name, age, sclass,  
  87.                             school);  
  88.                     Binddata();  
  89.                 }  
  90.             };  
  91.             OnClickListener click2 = new OnClickListener() {  
  92.   
  93.                 @Override  
  94.                 public void onClick(DialogInterface dialog, int which) {  
  95.                     // TODO Auto-generated method stub  
  96.   
  97.                 }  
  98.             };  
  99.             AlertDialog newstudentdialog = new AlertDialog.Builder(this)  
  100.                     .create();  
  101.             newstudentdialog.setView(myview);  
  102.   
  103.             newstudentdialog.setButton("确定", click1);  
  104.             newstudentdialog.setButton2("取消", click2);  
  105.             newstudentdialog.show();  
  106.             break;  
  107.         }  
  108.         return false;  
  109.   
  110.     }  
  111.   
  112.     public void Binddata() {  
  113.         mdbhelper = new MdbHelper(this);  
  114.         mdbhelper.open();  
  115.         String[] student = new String[] { KeyRowID, KeyRowname, KeyRowAge,  
  116.                 KeyRowClass, KeyRowSchool };  
  117.         int[] arraystudent = new int[] { R.id.studentid, R.id.studentname,  
  118.                 R.id.studentage, R.id.studentclass, R.id.studenschool };  
  119.         Cursor curs = mdbhelper.QueryStudentALl();  
  120.         startManagingCursor(curs);  
  121.         SimpleCursorAdapter ad = new SimpleCursorAdapter(this,  
  122.                 R.layout.list_row, curs, student, arraystudent);  
  123.         // setListAdapter(ad);如果继承ListActivity  
  124.         ListView list = (ListView) findViewById(R.id.list);  
  125.         list.setAdapter(ad);  
  126.   
  127.         OnItemClickListener clicklisterner = new OnItemClickListener() {  
  128.   
  129.             @Override  
  130.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  131.                     long arg3) {  
  132.                 // TODO Auto-generated method stub  
  133.                 Studentid= arg2 + 1;  
  134.                 Cursor simplestuent = mdbhelper.fetchMyTableById(Studentid);  
  135.                 while (simplestuent.moveToNext()) {  
  136.                     name = simplestuent.getString(1);  
  137.                     age = simplestuent.getString(2);  
  138.                     Class = simplestuent.getString(3);  
  139.                     School = simplestuent.getString(4);  
  140.                 }  
  141.                 LayoutInflater factory = LayoutInflater.from(studentdatabase.this);  
  142.                 View myview = factory.inflate(R.layout.addstundet, null);  
  143.                 studentname = (EditText) myview  
  144.                         .findViewById(R.id.insertstudentname);  
  145.                 studentage = (EditText) myview  
  146.                         .findViewById(R.id.insertstudentage);  
  147.                 studentclass = (EditText) myview  
  148.                         .findViewById(R.id.insertstudentclass);  
  149.                 studentschool = (EditText) myview  
  150.                         .findViewById(R.id.insertstudentschool);  
  151.                 studentname.setText(name);  
  152.                 studentage.setText(age);  
  153.                 studentclass.setText(Class);  
  154.                 studentschool.setText(School);  
  155. //              Toast.makeText(studentdatabase.this, "您当前选择的编号为:" + Studentid,  
  156. //                      Toast.LENGTH_SHORT).show();  
  157.                 OnClickListener click1 = new OnClickListener() {  
  158.                     @Override  
  159.                     public void onClick(DialogInterface dialog, int which) {  
  160.                         // TODO Auto-generated method stub  
  161.                         String name = studentname.getText().toString();  
  162.                         String age = studentage.getText().toString();  
  163.                         String sclass = studentclass.getText().toString();  
  164.                         String school = studentschool.getText().toString();  
  165.                         mdbhelper.open();  
  166.                         mdbhelper.UpdateStudent(Studentid, name, age, sclass, school);  
  167.                         Binddata();  
  168.                     }  
  169.                 };  
  170.                   
  171.                 OnClickListener click2 = new OnClickListener() {  
  172.   
  173.                     @Override  
  174.                     public void onClick(DialogInterface dialog, int which) {  
  175.                         // TODO Auto-generated method stub  
  176.   
  177.                     }  
  178.                 };  
  179.                 OnClickListener click3=new OnClickListener() {  
  180.                       
  181.                     @Override  
  182.                     public void onClick(DialogInterface dialog, int which) {  
  183.                         // TODO Auto-generated method stub  
  184.                         mdbhelper.open();  
  185.                         if(mdbhelper.deletestudent(Studentid))  
  186.                         {  
  187.                             Toast.makeText(studentdatabase.this"编号为"+Studentid+"学生删除成功", Toast.LENGTH_SHORT).show();  
  188.                             Binddata();  
  189.                         }  
  190.                     }  
  191.                 };  
  192.                 AlertDialog newstudentdialog = new AlertDialog.Builder(studentdatabase.this)  
  193.                         .create();  
  194.                 newstudentdialog.setView(myview);  
  195.                 newstudentdialog.setButton("更新", click1);  
  196.                 newstudentdialog.setButton2("取消", click2);  
  197.                 newstudentdialog.setButton3("删除", click3);  
  198.                 newstudentdialog.show();  
  199.   
  200.             }  
  201.         };  
  202.         list.setOnItemClickListener(clicklisterner);  
  203.         // mdbhelper.close();  
  204.     }  
  205. }  

mail.xml:

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <ListView android:id="@+id/list" android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" />  
  7. </LinearLayout>  

addstundet.xml:

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content">  
  5.   
  6.     <TableLayout android:id="@+id/TableLayout01"  
  7.         android:layout_width="wrap_content" android:layout_height="wrap_content">  
  8.         <TableRow android:id="@+id/TableRow01" android:orientation="horizontal"  
  9.             android:layout_width="wrap_content" android:layout_height="wrap_content">  
  10.   
  11.             <TextView android:id="@+id/txtname" android:layout_width="100px"  
  12.                 android:layout_marginLeft="20px" android:layout_height="wrap_content"  
  13.                 android:text="姓名"></TextView>  
  14.             <EditText android:id="@+id/insertstudentname"  
  15.                 android:layout_width="150px" android:layout_height="wrap_content"  
  16.                 android:text=""></EditText>  
  17.         </TableRow>  
  18.         <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content">  
  20.             <TextView android:id="@+id/txtage" android:layout_width="100px"  
  21.                 android:layout_marginLeft="20px" android:layout_height="wrap_content"  
  22.                 android:text="年龄"></TextView>  
  23.             <EditText android:id="@+id/insertstudentage"  
  24.                 android:layout_width="150px" android:layout_height="wrap_content"  
  25.                 android:text=""></EditText>  
  26.         </TableRow>  
  27.         <TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content">  
  29.             <TextView android:id="@+id/txtclass" android:layout_width="100px"  
  30.                 android:layout_marginLeft="20px" android:layout_height="wrap_content"  
  31.                 android:text="班级"></TextView>  
  32.             <EditText android:id="@+id/insertstudentclass"  
  33.                 android:layout_width="150px" android:layout_height="wrap_content"  
  34.                 android:text=""></EditText>  
  35.         </TableRow>  
  36.         <TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content">  
  38.             <TextView android:id="@+id/txtschool" android:layout_width="100px"  
  39.                 android:layout_marginLeft="20px" android:layout_height="wrap_content"  
  40.                 android:text="学校"></TextView>  
  41.             <EditText android:id="@+id/insertstudentschool"  
  42.                 android:layout_width="150px" android:layout_height="wrap_content"  
  43.                 android:text=""></EditText>  
  44.         </TableRow>  
  45.         <TableRow android:id="@+id/TableRow03" android:orientation="horizontal"  
  46.             android:layout_width="wrap_content" android:layout_height="wrap_content">  
  47.               
  48.             </TableRow>  
  49.     </TableLayout>  
  50. </LinearLayout>  

list_row.xml:

view plain
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content">  
  5.     <TextView   
  6.     android:text="学生编号"   
  7.     android:id="@+id/studentid"  
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content">  
  10.     </TextView>  
  11.     <LinearLayout  
  12.     xmlns="http://schemas.android.com/apk/res/android"  
  13.     android:orientation="horizontal"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="wrap_content"  
  16.     >  
  17.    <TextView  
  18.    android:id="@+id/studentname"  
  19.    android:layout_width="wrap_content"  
  20.    android:layout_height="wrap_content"  
  21.    android:text="学生姓名"  
  22.      android:layout_marginLeft="20px"  
  23.    >  
  24.    </TextView>  
  25.    <TextView android:id="@+id/studentage"  
  26.    android:layout_width="wrap_content"  
  27.    android:layout_height="wrap_content"  
  28.    android:text="学生年纪"  
  29.      android:layout_marginLeft="20px"  
  30.    ></TextView>  
  31.    <TextView  
  32.    android:id="@+id/studentclass"  
  33.    android:layout_width="wrap_content"  
  34.    android:layout_height="wrap_content"  
  35.    android:text="学生班级"  
  36.    android:layout_marginLeft="20px"  
  37.    >  
  38.    </TextView>  
  39.    <TextView android:id="@+id/studenschool"  
  40.    android:layout_width="wrap_content"  
  41.    android:layout_height="wrap_content"  
  42.    android:text="学生学校"  
  43.      android:layout_marginLeft="20px"  
  44.    ></TextView>  
  45.     </LinearLayout>  
  46. </LinearLayout>