danni

Meeting you is my big luck. Weak water three thousand, only take aspoonful happiness。

导航

创建sqlite数据库

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DBAdapter {
 public static final String KEY_ROWID = "chengyu";//列的字段名
 public static final String KEY_word = "pinyin";
 public static final String KEY_explain = "jieshi";
 public static final String KEY_date = "chuchu";
 public static final String KEY_shili = "shili";
 private static final String DATABASE_NAME = "data.db";//数据库名
 private static final String DATABASE_TABLE = "chengyucidian";//表名
 private static final int DATABASE_VERSION = 1;//版本号

    //创建数据的语句
 private static final String DATABASE_CREATE =
         "CREATE TABLE chengyucidian (chengyu text,pinyin text,jieshi text,chuchu text,shili text);";
 
 
 private final Context context;
 private DatabaseHelper DBHelper;
 private SQLiteDatabase db;

 public DBAdapter(Context ctx)
 {
  this.context = ctx;
  DBHelper = new DatabaseHelper(context);
 }

 private static class DatabaseHelper extends SQLiteOpenHelper
 {
  DatabaseHelper(Context context)
  {
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
 
  public void onCreate(SQLiteDatabase db)
  {
   db.execSQL(DATABASE_CREATE);
  }

  
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  {
   Log.w(null, "Upgrading database from version " + oldVersion+ " to "+ newVersion + ", which will destroy all old data");
   db.execSQL("DROP TABLE IF EXISTS titles");
   onCreate(db);
  }
 }

 public DBAdapter open() throws SQLException          //打开数据库
 {
  db = DBHelper.getReadableDatabase();
  return this;
 }

 public void close()                //关闭数据库
 {
  DBHelper.close();
  System.out.println("关闭数据库");
 }

 

 public Cursor getlikeword(String s,String s2,String s3,String s4,String s5,String s6,String s7,String s8)    //模糊查询
 { 
  
  String sql ="SELECT chengyu FROM chengyucidian WHERE chengyu like '%"+s+"%"+s2+"%"+s3+"%"+s4+"%"+s5+"%"+s6+"%"+s7+"%"+s8+"%'";
  Cursor cursor = db.rawQuery(sql, null);
  return cursor;
  
 }

 public Cursor getall(String s)           //查询所有
 { 
  
  String sql ="SELECT pinyin,jieshi,chuchu,shili FROM chengyucidian WHERE chengyu = '"+s+"'";
  Cursor cursor = db.rawQuery(sql, null);
  return cursor;
  
 }
 public long inserTitle(String word,String jieshi)    //向数据库中插入一个标题;
 {  //向数据库中插入一个标题
  ContentValues initialValues = new ContentValues();
  initialValues.put(KEY_ROWID, word);
  initialValues.put(KEY_explain, jieshi);

  return db.insert(DATABASE_TABLE, null, initialValues);
 }
 public Cursor query(String sql,String[] args)  
    {  
       
            //SQLiteDatabase db = this.getReadableDatabase();  
            Cursor cursor = db.rawQuery(sql, args);  
            return cursor;  
     }  

 

posted on 2012-05-21 16:56  danni  阅读(1328)  评论(0编辑  收藏  举报