• SQLite数据库操作


    在实际开发中经常会用到数据库的操作,这里简单总结下,也可以直接拿来用。

    DatabaseHelper.java

     1 package com.serial.db;
     2 
     3 import android.content.ContentValues;
     4 import android.content.Context;
     5 import android.database.sqlite.SQLiteDatabase;
     6 import android.database.sqlite.SQLiteDatabase.CursorFactory;
     7 import android.database.sqlite.SQLiteOpenHelper;
     8 
     9 public class DatabaseHelper extends SQLiteOpenHelper {
    10 
    11     private static final String DB_NAME = "check_data.db"; //数据库名称
    12     private static final int version = 1; //数据库版本
    13     public static final String TABLE_NAME = "mydata"; 
    14     
    15     public DatabaseHelper(Context context) {
    16         super(context, DB_NAME, null, version);
    17         
    18     }
    19  
    20     @Override
    21     public void onCreate(SQLiteDatabase db) {
    22         String sql = "create table "+TABLE_NAME+"(username varchar(20) not null , password varchar(60) not null );";          
    23         db.execSQL(sql);
    24     }
    25  
    26     @Override
    27     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    28        
    29  
    30     }
    31     
    32      
    33 }

    DBUtils.java

     1 package com.serial.db;
     2 
     3 import android.content.ContentValues;
     4 import android.content.Context;
     5 import android.database.Cursor;
     6 import android.database.sqlite.SQLiteDatabase;
     7 
     8 public class DBUtils {
     9 
    10     SQLiteDatabase db = null;
    11     DatabaseHelper database = null;
    12     
    13     public DBUtils(Context context){
    14         
    15         database = new DatabaseHelper(context);
    16         db = database.getReadableDatabase();
    17         
    18     }
    19     
    20     //插入数据方式,两种
    21     public void insertContent(ContentValues cv){
    22         
    23         db.insert(DatabaseHelper.TABLE_NAME,null,cv);//执行插入操作
    24     }
    25     
    26     public void insertCommon(String strSql){
    27         
    28         db.execSQL(strSql);//执行SQL语句
    29     }
    30     
    31     //删除数据方式,两种
    32     public void deleteArgs(String whereClause, String[] whereArgs){
    33         
    34         db.delete(DatabaseHelper.TABLE_NAME,whereClause,whereArgs);//执行删除
    35     }
    36     
    37     public void deleteSql(String delSql){
    38         
    39         db.execSQL(delSql);//执行删除操作
    40     }
    41     
    42     //修改数据的方式, 两种
    43     public void changeContent(ContentValues cv, String whereClause, String[] whereArgs ){
    44         
    45         db.update(DatabaseHelper.TABLE_NAME,cv,whereClause,whereArgs);//执行修改
    46     }
    47     
    48     public void changeCommon(String updateSql){
    49         
    50         db.execSQL(updateSql);//执行修改
    51     }
    52     
    53     //查询方式,两种
    54     public String searchCursor(){
    55         String username = "-1", password = "-1";
    56         Cursor c = db.query(DatabaseHelper.TABLE_NAME,null,null,null,null,null,null);//查询并获得游标
    57         if(c.moveToFirst()){//判断游标是否为空
    58             for(int i=0;i<c.getCount();i++){
    59                 c.move(i);//移动到指定记录
    60                 username = c.getString(c.getColumnIndex("username"));
    61                 password = c.getString(c.getColumnIndex("password"));
    62             }
    63         }
    64         
    65         return username + "," + password;
    66     }
    67     
    68     public String searchRawQuery(){
    69         
    70         String username = "-1", password = "-1";
    71         Cursor cursor = db.rawQuery("select * from "+DatabaseHelper.TABLE_NAME+" where username=?",new String[]{"Jack Johnson"});
    72         if(cursor.moveToFirst()) {
    73             username = cursor.getString(cursor.getColumnIndex("username"));
    74             password = cursor.getString(cursor.getColumnIndex("password"));
    75         }
    76         
    77         return username + "-" + password;
    78     }
    79 }

    参考:http://www.cnblogs.com/Excellent/archive/2011/11/19/2254888.html

  • 相关阅读:
    模式匹配算法Index
    模式匹配算法Index
    模式匹配算法Index
    css中“~”(波浪号)、“,”(逗号)、 “ + ”(加号)和 “ > ”(大于号)是什么意思?
    ACM向:关于优先队列priority_queue自定义比较函数用法整理
    ACM向:关于优先队列priority_queue自定义比较函数用法整理
    Bootstrap 插件轮播
    Bootstrap 插件轮播
    ASP.NET 控件中AutoPostBack属性
    ASP.NET 控件中AutoPostBack属性
  • 原文地址:https://www.cnblogs.com/Miami/p/4535101.html
Copyright © 2020-2023  润新知