Android SQlite实现简单的添加、查找、删除

SQlite简介(自己不擅长描述,参考https://blog.csdn.net/yanbober/article/details/45567149)

SQLite是一款轻型的数据库,是关系型数据库(RDBMS)管理系统,它包含在一个相对小的C库中。目前在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix/Android/IOS等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。

原创部分:

我项目中一共用到了三四个表,基本上大同小异,就挑出一个描述一下,垃圾代码如下


AccountInformationDatebase.java

 
 
package database.operate;

import java.util.ArrayList;
import java.util.List;

import object.AccountInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;
public class AccountInformationDatabase {
	static final String KEY_ROWID = "_id";  
	static final String KEY_APPNAME = "SOFTNAME";
    static final String KEY_NUMBER = "NUMBER";    //账号
    static final String KEY_PASSWORD = "PASSWORD";  //密码

    static final String TAG = "AccountInformationDatabase";  
      
    static final String DATABASE_NAME = "InformationDatabase";  
    static final String DATABASE_TABLE = "AccountInformationTABLE";  
    static final int DATABASE_VERSION = 1;  
      
    static final String DATABASE_CREATE =   
            "create table if not exists FileInformationTABLE( _id integer primary key autoincrement, " +   
            "FILENAME text not null, fileSHA3 text not null, PASSWORD text not null);";  
    
    static final String DATABASE_AccountCREATE =   
            "create table if not exists AccountInformationTABLE( _id integer primary key autoincrement, " +   
            "NUMBER text not null, PASSWORD text not null, SOFTNAME text not null);";  
   
    static final String DATABASE_ClientCREATE =   
            "create table if not exists ClientInformationTABLE( _id integer primary key autoincrement, " +   
            "ClientNAME text not null, ClientIP text not null, ClientMAC text not null);";  
    
    static final String DATABASE_ShareCREATE =   
            "create table if not exists ShareInformationTABLE( _id integer primary key autoincrement, " +   
            "ShareFILENAME text not null, SharefileSHA3 text not null, SharePASSWORD text not null);"; 
    
    static final String DATABASE_MasterPrivateCREATE =   
            "create table if not exists MasterPrivateTABLE( _id integer primary key autoincrement, " +   
            "PROPERTY text not null, PRIVATEKEY text not null);"; 
    
    static final String DATABASE_MasterPublicCREATE =   
            "create table if not exists MasterPublicTABLE( _id integer primary key autoincrement, " +   
            "MASTERKEY text not null, PUBLICKEY text not null);"; 
    
    static final String DATABASE_MinorPrivateCREATE =   
            "create table if not exists MinorPrivateTABLE( _id integer primary key autoincrement, " +   
            "PROPERTY text not null, PRIVATEKEY text not null);"; 
    
    final Context context;  
      
    DatabaseHelper DBHelper;  
    SQLiteDatabase db;  
      
    public AccountInformationDatabase(Context cxt)  
    {  
        this.context = cxt;  
        DBHelper = new DatabaseHelper(context);  
    }  
      
    public static class DatabaseHelper extends SQLiteOpenHelper  
    {  
  
        DatabaseHelper(Context context)  
        {  
            super(context, DATABASE_NAME, null, DATABASE_VERSION);  
            SQLiteDatabase.loadLibs(context);
        }  
        @Override  
        public void onCreate(SQLiteDatabase db) {  
            // TODO Auto-generated method stub  
            try  
            {  
                db.execSQL(DATABASE_AccountCREATE);  //add table
                db.execSQL(DATABASE_ShareCREATE); 
                db.execSQL(DATABASE_CREATE);  //add table
                db.execSQL(DATABASE_ClientCREATE);    //add table
            	db.execSQL(DATABASE_MinorPrivateCREATE); 
            	db.execSQL(DATABASE_MasterPublicCREATE); 
            	db.execSQL(DATABASE_MasterPrivateCREATE);
            }  
            catch(SQLException e)  
            {  
                e.printStackTrace();  
            }  
        }  
  
        @Override  
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
            // TODO Auto-generated method stub  
            db.execSQL("DROP TABLE IF EXISTS AccountInformationTABLE"); 
            onCreate(db);  
        }  
    }  
      
    //open the database  
    public AccountInformationDatabase open(String key) throws SQLException  
    {  
        db = DBHelper.getWritableDatabase(key);  
        return this;  
    }  
    //close the database  
    public void close()  
    {  
        DBHelper.close();  
    }  
      
    //insert a contact into the database  
    public long insertContact(String number, String password, String softname)  
    {  
    	//查询插入是否存在
    	if(find(number,softname)==null){  //插入数据不存在
        ContentValues initialValues = new ContentValues();  
        initialValues.put(KEY_NUMBER, number);  
        initialValues.put(KEY_PASSWORD, password);  
        initialValues.put(KEY_APPNAME, softname);
        return db.insert(DATABASE_TABLE, null, initialValues);  
        }
    	else
    		return -1;
    }  
    //delete a particular contact  
    public boolean deleteContact(String number,String softname)  
    {  
    	if(find(number,softname)!=null){  //数据存在
    		return db.delete(DATABASE_TABLE, "NUMBER=? and SOFTNAME=?", new String[]{number,softname}) > 0;
    	}
    	else
    		return false;
    }  
    
    public AccountInfo find(String number,String softname){   //精确查询
        //如果只对数据进行读取,建议使用此方法  
        Cursor cursor = db.rawQuery("select * from  AccountInformationTABLE where NUMBER=? and SOFTNAME=?", new String[]{number,softname});//得到游标  
        if(cursor.moveToFirst()){  
            String NUMBER = cursor.getString(cursor.getColumnIndex("NUMBER"));  
            String PASSWORD = cursor.getString(cursor.getColumnIndex("PASSWORD"));  
            String SOFTNAME=cursor.getString(cursor.getColumnIndex("SOFTNAME"));
            AccountInfo person = new AccountInfo(NUMBER, PASSWORD, SOFTNAME);  
            return person;  
        }
        else 
        	 return null;  //查询失败,无相关数据
    }  
    
    public List<AccountInfo> getScrollData(){    //返回全部数据
    	
        List<AccountInfo> AccountList = new ArrayList<AccountInfo>();  
        Cursor cursor = db.rawQuery("select * from  AccountInformationTABLE where PASSWORD like?", new String[]{"%%"});//得到游标  
        while(cursor.moveToNext()){  
        	 String number = cursor.getString(cursor.getColumnIndex("NUMBER"));  
             String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));  
             String softname = cursor.getString(cursor.getColumnIndex("SOFTNAME"));
             AccountInfo person = new AccountInfo(number, password, softname);   
             AccountList.add(person);  
        }  
        cursor.close();  
        return AccountList;  
    }  


    public boolean updateContact(String number, String password, String softname)  
    { 
    	if(find(number,softname)!= null){  //插入数据存在
    		ContentValues args = new ContentValues();  
    		args.put(KEY_NUMBER, number);  
    		args.put(KEY_PASSWORD, password);  
    		args.put(KEY_APPNAME, softname);  
    		return db.update(DATABASE_TABLE, args, KEY_NUMBER + " = ? and SOFTNAME = ?", new String[]{number,softname}) > 0;  
    	}
    	else
    		return false;
    }  
}

做一个简单的调用,EditText录入文本存储到SQlite中。

	Account = et1.getText().toString();
	Password = et2.getText().toString();

	AccountInformationDatabase account =new AccountInformationDatabase(SaftyBox_AddAccount.this);
	account.open("ahjdabjk");//SQLite密钥

	AccountCipher = Encryption.deal(Account,1,key);
	PasswordCipher = Encryption.deal(Password,1,key); 
        //这里是用SMS4对文本加密过后在存到SQLite中的

	account.insertContact(AccountCipher, PasswordCipher, Application);		
	Toast.makeText(getApplicationContext(), "添加成功!",Toast.LENGTH_SHORT).show();
	account.close();

他的表类似,不多做解释。如有问题,欢迎大佬指点。




猜你喜欢

转载自blog.csdn.net/cugzhaolc/article/details/80446753