SQLite使用方法 SQLiteOpenHelper操作(转)

SQLiteOpenHelper主要用于 创建数据库

SQLiteDatabase 主要用于 执行sql语句

  1. 程序内使用SQLite数据库是通过SQLiteOpenHelper进行操作  
  2. 1.       自己写个类继承SQLiteOpenHelper,重写以下3个方法  
  3. public void onCreate(SQLiteDatabase db)   
  4. {//创建数据库时的操作,如建表}  
  5.    
  6. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)   
  7.        {  
  8.            //版本更新的操作  
  9.        }  
  10. 2.    通过SQLiteOpenHelper的getWritableDatabase()获得一个SQLiteDatabase数据库,以后的操作都是对SQLiteDatabase进行操作。  
  11. 3.       对得到的SQLiteDatabase对象进行增,改,删,查等操作。  
  12. 代码  
  13. package cx.myNote;  
  14.    
  15. import android.content.ContentValues;  
  16. import android.content.Context;  
  17. import android.content.Intent;  
  18. import android.database.Cursor;  
  19. import android.database.sqlite.SQLiteDatabase;  
  20. import android.database.sqlite.SQLiteOpenHelper;  
  21.    
  22. //DBOptions for login  
  23. public class DBOptions {  
  24.        private static final String DB_NAME = "notes.db";  
  25.        private static final String DB_CREATE="create table logininf(name text,pwd text)";  
  26.        public class DBHelper extends SQLiteOpenHelper  
  27.        {  
  28.    
  29.               public DBHelper(Context context) {  
  30.                      super(context,DB_NAME, null1);  
  31.                      }  
  32.    
  33.               @Override  
  34.               public void onCreate(SQLiteDatabase db) {  
  35.                      // TODO Auto-generated method stub  
  36.                      //建表  
  37.                  db.execSQL(DB_CREATE);  
  38.               }  
  39.                 
  40.               @Override  
  41.               public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  42.                      // TODO Auto-generated method stub  
  43.                      db.execSQL("drop table if exists logininf");  
  44.                      onCreate(db);  
  45.               }  
  46.                 
  47.        }  
  48.        private Context context;  
  49.        private SQLiteDatabase db;  
  50.        private DBHelper dbHelper;  
  51.        public  DBOptions(Context context)  
  52.        {  
  53.               this.context = context;  
  54.               dbHelper = new DBHelper(context);  
  55.               db=dbHelper.getReadableDatabase();  
  56.                 
  57.        }  
  58.   //自己写的方法,对数据库进行操作  
  59.        public String getName()  
  60.        {  
  61.                 
  62.               Cursor cursor = db.rawQuery("select name from logininf"null);  
  63.               cursor.moveToFirst();  
  64.               return cursor.getString(0);       
  65.        }  
  66.        public int changePWD(String oldP,String pwd)  
  67.        {  
  68.               ContentValues values = new ContentValues();  
  69.               values.put("pwd", pwd);  
  70.               return db.update("logininf", values,"pwd="+oldP, null);  
  71.        }  
  72. }  

insert方法插入的一行记录使用ContentValus存放,ContentValues类似于Map,它提供了put(String key, Xxx value)(其中key为数据列的列名)方法用于存入数据、getAsXxxx(String key)方法用于取出数据。

转载自 http://blog.csdn.net/wangqilin8888/article/details/7780228

原文地址:https://www.cnblogs.com/YangBinChina/p/3998628.html