这是Java文件,类名是DatabaseHelpepackage com.example.database;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.view.Menu;public class DatabaseHelper extends SQLiteOpenHelper{private static final int VERSION=1;public DatabaseHelper(Context context,String name,CursorFactory factory,int version){ super(context,name,factory,version); }public DatabaseHelper(Context context,String name){ this(context,name,VERSION); }public DatabaseHelper(Context context,String name,int version){ this(context,name,null,version); } public void onCreate(SQLiteDatabase db) { System.out.println("create a Database"); db.execSQL("create table user(id int,name varchar(20))"); } public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) { System.out.println("update a Database"); }}这是另一个Java文件,类名是SQLiteActivitypackage com.example.database;import android.app.Activity;import android.content.ContentValues;import android.content.DialogInterface.OnClickListener;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SQLiteActivity extends Activity{private Button button1;private Button button2;private Button button3;private Button button4;private Button button5;protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.main); button1=(Button) findViewById(R.id.button1); button2=(Button) findViewById(R.id.button2); button3=(Button) findViewById(R.id.button3); button4=(Button) findViewById(R.id.button4); button5=(Button) findViewById(R.id.button5); button1.setOnClickListener(new CreatdbOnClickListener()); button2.setOnClickListener(new UpdatedbOnClickListener()); button3.setOnClickListener(new InsertOnClickListener()); button4.setOnClickListener(new UpdatedbOnClickListener()); button5.setOnClickListener(new QuerryOnClickListener());}class CreatdbOnClickListener implements OnClickListener{ public void onClick(View v){ DatabaseHelper dbHelper=new DatabaseHelper(SQLiteActivity.this,"test.db"); SQLiteDatabase db=dbHelper.getReadableDatabase(); } }class UpdateOnClickListener implements OnClickListener{ public void onClick(View v){ DatabaseHelper dbHelper=new DatabaseHelper(SQLiteActivity.this,"test.db",2); SQLiteDatabase db=dbHelper.getReadableDatabase(); }}class InsertOnClickListener implements OnClickListener{ public void onClick(View v){ ContentValues cv=new ContentValues(); cv.put("id", 1); cv.put("name", "hyl"); DatabaseHelper dbHelper=new DatabaseHelper(SQLiteActivity.this,"test.db",2); SQLiteDatabase db=dbHelper.getReadableDatabase(); db.insert("user",null,cv); } }class UpdatOnClickListener implements OnClickListener{ public void onClick(View v){ DatabaseHelper dbHelper=new DatabaseHelper(SQLiteActivity.this,"test.db",2); SQLiteDatabase db=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name","heyulu"); db.update("user",values,"id=?",new String[] {"1"}); System.out.println("----------------update------------"); } }class QuerryOnClickListener implements OnClickListener{ public void onClick(View v){ DatabaseHelper dbHelper=new DatabaseHelper(SQLiteActivity.this,"test.db",2); SQLiteDatabase db=dbHelper.getWritableDatabase(); 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("query--->"+name); } }}}还有一个xml文件<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"><Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/creatdb" /><Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/updatedb" /><Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/ins" /><Button android:id="@+id/button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/upd" /><Button android:id="@+id/button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/que" /></LinearLayout>
添加回答
举报
0/150
提交
取消