• 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

  • 相关阅读:
    可能不知道的C#特性
    设计模式の依赖注入
    How to find WWN and WWPN of HBA card in Linux
    fio IO测试工具
    centos/redhat 多路径存储使用 客户端
    centos/redhat 系统误删除逻辑卷之后如何恢复
    How to use lspci, lsscsi, lsusb, and lsblk to get Linux system devices information
    How to Check and Repair EXT4 Filesystem in Linux
    如何在 Linux 上扫描/检测新的 LUN 和 SCSI 磁盘
    小程序开发知识点总结归纳
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/3998628.html
Copyright © 2020-2023  润新知