Chinaunix首页 | 论坛 | 博客
  • 博客访问: 711149
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: Java

2011-08-22 23:30:01

  1. package cn.youhap.sqlite;

  2. import java.io.File;

  3. import android.app.Activity;
  4. import android.content.ContentValues;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;

  11. public class SqliteTestDrive extends Activity {
  12.     Button addButton;
  13.     Button deleteButton;
  14.     Button updateButton;
  15.     Button createDBButton;
  16.     Button dropDBButton;
  17.     Button queryButton;
  18.     
  19.     
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         
  24.         setContentView(R.layout.main);
  25.         
  26.         createDBButton = (Button)findViewById(R.id.createDBButton);
  27.         addButton = (Button)findViewById(R.id.addButton);
  28.         deleteButton = (Button)findViewById(R.id.deleteButton);
  29.         updateButton = (Button)findViewById(R.id.updateButton);
  30.         
  31.         dropDBButton = (Button)findViewById(R.id.dropDBButton);
  32.         queryButton = (Button)findViewById(R.id.queryButton);
  33.         
  34.         createDBButton.setOnClickListener(new CreateDatabaseListener());
  35.         addButton.setOnClickListener(new AddDatabaseListener());
  36.         updateButton.setOnClickListener(new UpdateDatabaseListener());
  37.         queryButton.setOnClickListener(new QueryDatabaseListener());
  38.         deleteButton.setOnClickListener(new DeleteDatabaseListener());
  39.         dropDBButton.setOnClickListener(new DropDatabaseListener());
  40.     }
  41.     
  42.     
  43.     class CreateDatabaseListener implements OnClickListener{

  44.         @Override
  45.         public void onClick(View v) {
  46.             DatabaseHelper helper = new DatabaseHelper(SqliteTestDrive.this, "test_database");
  47.             helper.getWritableDatabase();
  48.         }
  49.         
  50.     }
  51.     
  52.     class AddDatabaseListener implements OnClickListener{

  53.         @Override
  54.         public void onClick(View v) {
  55.             DatabaseHelper helper = new DatabaseHelper(SqliteTestDrive.this, "test_database");
  56.             SQLiteDatabase db = helper.getWritableDatabase();
  57.             ContentValues value = new ContentValues();
  58.             value.put("id", 1);
  59.             value.put("name", "liujun");
  60.             db.insert("user", null, value);
  61.         }
  62.         
  63.     }
  64.     
  65.     class UpdateDatabaseListener implements OnClickListener{

  66.         @Override
  67.         public void onClick(View v) {
  68.             DatabaseHelper helper = new DatabaseHelper(SqliteTestDrive.this, "test_database");
  69.             SQLiteDatabase db = helper.getWritableDatabase();
  70.             ContentValues value = new ContentValues();
  71.             value.put("name", "whm");
  72.             db.update("user", value, "id=?", new String [] {"1"});
  73.         }
  74.         
  75.     }
  76.     
  77.     class QueryDatabaseListener implements OnClickListener{

  78.         @Override
  79.         public void onClick(View v) {
  80.             DatabaseHelper helper = new DatabaseHelper(SqliteTestDrive.this, "test_database");
  81.             SQLiteDatabase db = helper.getReadableDatabase();
  82.             
  83.             db.query("user", new String [] {"name"}, "id=?", new String [] {"1"}, null, null, null);
  84.             
  85.             Cursor cursor = db.query("user", new String [] {"id", "name"}, "id=?", new String [] {"1"}, null, null, null);
  86.             while (cursor.moveToNext()){
  87.                 System.out.println(cursor.getString(cursor.getColumnIndex("name")));
  88.             }
  89.         }
  90.         
  91.     }
  92.     
  93.     class DeleteDatabaseListener implements OnClickListener{

  94.         @Override
  95.         public void onClick(View v) {
  96.             DatabaseHelper helper = new DatabaseHelper(SqliteTestDrive.this, "test_database");
  97.             SQLiteDatabase db = helper.getWritableDatabase();
  98.             db.delete("user", "id=?", new String [] {"1"});
  99.         }
  100.         
  101.     }

  102.     
  103.     class DropDatabaseListener implements OnClickListener{

  104.         @Override
  105.         public void onClick(View v) {
  106.             DatabaseHelper helper = new DatabaseHelper(SqliteTestDrive.this, "test_database");
  107.             SQLiteDatabase db = helper.getWritableDatabase();
  108.             
  109.             db.close();
  110.             String packageName = "/data/data/" + this.getClass().getPackage().getName();
  111.             File file = new File(packageName + "/databases/test_database");
  112.             if (file.exists()){
  113.                 boolean b = file.delete();
  114.                 System.out.println(b);
  115.             }else{
  116.                 System.out.println("can not found the file.");
  117.             }
  118.         }
  119.         
  120.     }
  121. }


  1. package cn.youhap.sqlite;

  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  5. import android.database.sqlite.SQLiteOpenHelper;

  6. public class DatabaseHelper extends SQLiteOpenHelper {
  7.     
  8.     private static final int VERSION = 1;
  9.     public DatabaseHelper(Context context, String name, CursorFactory factory,
  10.             int version) {
  11.         super(context, name, factory, version);
  12.     }
  13.     
  14.     
  15.     public DatabaseHelper(Context context, String name, int version){
  16.         this(context, name, null, version);
  17.     }
  18.     
  19.     public DatabaseHelper(Context context, String name){
  20.         this(context, name, VERSION);
  21.     }
  22.     
  23.     @Override
  24.     public void onCreate(SQLiteDatabase db) {
  25.         db.execSQL("create table user(id int(10), name varchar(20))");
  26.         System.out.println("-------------------------->table is created.");
  27.     }
  28.     
  29.     @Override
  30.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  31.         System.out.println("-------------------------->update database.");
  32.     }
  33.     
  34. }

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView
  8.     android:layout_width="fill_parent"
  9.     android:layout_height="wrap_content"
  10.     />
  11. <Button android:text="create database" android:id="@+id/createDBButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  12. <Button android:text="add" android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  13. <Button android:text="update" android:id="@+id/updateButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  14. <Button android:text="query" android:id="@+id/queryButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  15. <Button android:text="delete" android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  16. <Button android:text="drop database" android:id="@+id/dropDBButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  17. </LinearLayout>
阅读(908) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~