zoukankan      html  css  js  c++  java
  • android 本地数据库sqlite的封装

      单机android   sqlite数据库的实现,这个数据库可与程序一起生成在安装包中

    一、下载sqlite3.exe文件

    二、运行 cmd 转到sqlite3.exe 所在目录  运行 sqlite3.exe 数据库名.db

        然后会出现sqlite>的命令提示符

        输入创建表的语句, create table 表名(‘列’,‘列’。。。);(注意: 要在结束部分加  分号 )

        此时会在sqlite3.exe 所在目录,出现所建数据库的文件

    三、如果想在Android中运行的话,需要在数据库中增添

     CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'zh_CN')
    INSERT INTO "android_metadata" VALUES ('zh_CN')

    四、将数据库 复制到 Android项目中res/raw中

    五、下面是代码:
    public class TestSqlDatabase{
    	
    	private static final String DATABASE_PATH = "/data/data/your.package.name/databases";     //此处不要改动,这个为数据库在手机上的物理地址
    	private static final int DATABASE_VERSION = 0;
    	private static final String DATABASE_NAME = "test.db";  //此处为数据库名称
    	
    	private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME;
    	
    	private Context context;
         private SQLiteDatabase database;
    	
    	public TestSqlDatabase(Context context) {
    		this.context = context;
    		
    		File file = new File(outFileName);
    		if (file.exists()) {
    			database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);
    			if (database.getVersion() != DATABASE_VERSION) {
    				database.close();
    				file.delete();	
    			}
    		}
    		try {
    			buildDatabase();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    	}
    	private void buildDatabase() throws Exception{
    		InputStream myInput = context.getResources().openRawResource(R.raw.test);
    		File file = new File(outFileName);
    		
    		File dir = new File(DATABASE_PATH);
    		if (!dir.exists()) {
    			if (!dir.mkdir()) {
    				throw new Exception("创建失败");
    			}
    		}
    		
    		if (!file.exists()) {			
    			try {
    				OutputStream myOutput = new FileOutputStream(outFileName);
    				
    				byte[] buffer = new byte[1024];
    		    	int length;
    		    	while ((length = myInput.read(buffer))>0){
    		    		myOutput.write(buffer, 0, length);
    		    	}
    		    	myOutput.close();
    		    	myInput.close();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		
    		}
    	}
    
    /**
     * 查找
     * @return
     */
    public Cursor select() {
         database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);
         String sql = "select * from note_table";
    		
         Cursor cursor = database.rawQuery(sql, null);
         return cursor;
    }
    /**
     * 插入
     * @param word
     * @param note
     * @return
     */
    public long insert(String word, String note) {
          database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);
          ContentValues cv = new ContentValues();
          cv.put("word", word);
          cv.put("note", note);
    			
          long result = database.insert("note_table", null, cv);	
          return result;
    }
     
     
    
    /**
     * 更新
     * @param word
     * @param note
     * @return
    	 */
    	private int update(String word, String note) {                            //参数 word 为修改条件   note为修改内容
    		database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);
    		
    		ContentValues cv = new ContentValues();
    		cv.put("note", note);
    		
    		int result = database.update("note_table", cv, "word=?", new String[]{word});     
    		
    		return result;
    	}
     
    
    /**
     * 删除
     * @param word
     */
    public int deleteNote(String word) {
          database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);
          int result = database.delete("note_table", "word=?", new String[]{word});
          return result;
    }
     
        
    public void close() {
          database.close();
    }
    
    
    }
     
    

      

      

  • 相关阅读:
    [ZJOI2007]时态同步 题解
    Xposed 在android 6.0上报couldn't load class,找不到xposed_init中配置的入口类
    微信小程序http 400问题
    在Mac上 python中使用tesseract OCR (Pytesser) 识别图片中的文字
    微信小游戏跳一跳简单手动外挂(基于adb 和 python)
    第一个微信小程序踩的几个小坑
    android studio/Intellij IDEA(MAC OSX)中android模拟器无法启动的一种原因
    【转载】word2vec原理推导与代码分析
    HTTP Get Post究竟有哪些区别
    初试kotlin:用Kotlin开发桌面/CommandLine 工具
  • 原文地址:https://www.cnblogs.com/lizhiyan-world/p/3740351.html
Copyright © 2011-2022 走看看