Android SQLite数据库简单示例


解决

SqLiteActivity.java:


package com.cuit.edu.cn;  import com.cuit.edu.cn.db.DateBaseHelper;  import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;  public class SqLiteActivity extends Activity {     /** Called when the activity is first created. */          private Button createButton = null;     private Button updatebaseButton = null;     private Button insertButton = null;     private Button updateButton = null;     private Button queryButton = null;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  createButton = (Button)findViewById(R.id.createdatebase);         updatebaseButton = (Button)findViewById(R.id.updateDatebase);         insertButton = (Button)findViewById(R.id.insterButton);         updateButton = (Button)findViewById(R.id.updateButton);         queryButton = (Button)findViewById(R.id.queryButton);                           createButton.setText(R.string.create);         updatebaseButton.setText(R.string.updatebase);         insertButton.setText(R.string.insert);         updateButton.setText(R.string.update);         queryButton.setText(R.string.query);                  createButton.setOnClickListener(new createButtonListener());         updatebaseButton.setOnClickListener(new updatebaseButtonListener());         insertButton.setOnClickListener(new insertButtonListener());         updateButton.setOnClickListener(new updateButtonListener());         queryButton.setOnClickListener(new queryButtonListener());                                }          class createButtonListener implements OnClickListener     {          @Override         public void onClick(View v) {             // TODO Auto-generated method stub             DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base");             SQLiteDatabase db = dbHelper.getReadableDatabase();         }              }          class updatebaseButtonListener implements OnClickListener     {          @Override         public void onClick(View v) {             // TODO Auto-generated method stub          System.out.println("b================");             DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base",90);                          SQLiteDatabase db = dbHelper.getReadableDatabase();                          dbHelper.onUpgrade(db, 90, 91);                      }              }          class insertButtonListener implements OnClickListener     {          @Override         public void onClick(View v) {             // TODO Auto-generated method stub             ContentValues values = new ContentValues();             values.put("id", 1);             values.put("name", "rock");             DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this,"user_Base");             SQLiteDatabase db = dbHelper.getWritableDatabase();             db.insert("user", null, values);         }              }          class updateButtonListener implements OnClickListener     {          @Override         public void onClick(View arg0) {             // TODO Auto-generated method stub             DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base");             SQLiteDatabase db = dbHelper.getWritableDatabase();             ContentValues values = new ContentValues();             values.put("name", "cara");             db.update("user", values, "id=?", new String[]{"1"});         }              }     class queryButtonListener implements OnClickListener{          @Override         public void onClick(View v) {             // TODO Auto-generated method stub             DateBaseHelper dbHelper = new DateBaseHelper(SqLiteActivity.this, "user_Base");             SQLiteDatabase db = dbHelper.getReadableDatabase();             Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);             while(cursor.moveToNext())             {                 String name = cursor.getString(cursor.getColumnIndex("name"));                 System.out.println("------"+name);             }                          }              } }


DateBaseHelper.java:


package com.cuit.edu.cn.db;  import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper;  public class DateBaseHelper extends SQLiteOpenHelper {      private static final int VERSION = 1;     public DateBaseHelper(Context context, String name, CursorFactory factory,             int version) {         super(context, name, factory, version);         // TODO Auto-generated constructor stub     }          public DateBaseHelper(Context context,String name)     {         this(context,name,VERSION);     }          public DateBaseHelper(Context context,String name,int version)     {         this(context,name,null,VERSION);     }      @Override     public void onCreate(SQLiteDatabase db) {         // TODO Auto-generated method stub         System.out.println("createdataBase");                  db.execSQL("create table user(id int, name varchar(20))");     }      @Override     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {         // TODO Auto-generated method stub         System.out.println("updataBase");     }  }



main.xml:


<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical" >      <TextView         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/hello" />     <Button         android:id="@+id/createdatebase"          android:layout_width="fill_parent"         android:layout_height="wrap_content"                  />          <Button         android:id="@+id/updateDatebase"          android:layout_width="fill_parent"         android:layout_height="wrap_content"         />          <Button         android:id="@+id/insterButton"          android:layout_width="fill_parent"         android:layout_height="wrap_content"         />         <Button         android:id="@+id/updateButton"          android:layout_width="fill_parent"         android:layout_height="wrap_content"                  />         <Button         android:id="@+id/queryButton"          android:layout_width="fill_parent"         android:layout_height="wrap_content"                  />           </LinearLayout>


string.xml:


<?xml version="1.0" encoding="utf-8"?> <resources>      <string name="hello">Hello World, SqLiteActivity!</string>     <string name="app_name">SqLite</string>     <string name="query">query</string>     <string name="updatebase">updatebase</string>     <string name="update">update</string>     <string name="create">create</string>     <string name="insert">insert</string>       </resources>