填充SQLite数据库

编程入门 行业动态 更新时间:2024-10-23 15:23:46
本文介绍了填充SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不是很熟悉Android的SQLite数据库。我只有填充SQLite数据库的大概的了解。我按照一些教程,但他们告诉不同的东西。 我有一个Android项目和它的packeges之一是名为.db 该套件包括5个不同的类别。它们是:

I am not very much familiar with android sqlite database. I only have rough idea of populating sqlite database. I have followed some tutorials but they tells different things. I have an android project and one of its' packeges is a .db This package consists of 5 different classes. They are:

  • DataAdapter.java
  • DataBaseHelper.java
  • DBAdapter.java
  • LoadDBActivity.java
  • SelectDBAdapter.java
  • 我知道SelectDBAdapter类是用来选择数据库中的数据。我的数据库是这是.JPEG format.I可以从sqliteBrowser打开资源文件夹。 其实,我想知道的是为什么要使用这些不同的类?什么是每个类的目的是什么?

    I know SelectDBAdapter class is used to select data from the database. My database is in asset folder which is in .jpeg format.I can open it from sqliteBrowser. Actually, what I want to know is why should we use these different classes ? and what's the purpose of each and every class ?

    我真的很抱歉,我不能发表codeS,因为这个项目是属于另外一个人(我的朋友)。 我将非常感谢,如果有人可以这么好心足以说明使用这些不同类别的含义,我们为什么要使用这样的塞纳里奥?

    I am really sorry, I cannot post codes since this projects belongs to another person (my friend). I would be much obliged if anyone could be so kind enough to explain the meaning of using these different classes and why should we use such a senario ?

    推荐答案

    从我的开发经验,我总是preFER在/ RES加prepared SQLite数据库文件/原folder.You创建/管理使用Firefox的SQLite的经理插件SQLite数据库,这是一个伟大的工具。这种方法实在是太棒了,因为

    From my development experience , I always prefer to add a prepared sqlite database file in the /res/raw folder.You create/manage sqlite database using Sqlite Manager addon of Firefox , it's a great tool. This method is really great because

    • 首先,我并不需要写C $ CS一堆$创建/管理数据库。
    • 最重要的是,一些应用程序需要从pre-填充数据库中读取。我并不需要关心什么应用程序需要和数据库是否为空或者已经填满。它为所有的目的。我只需要编写的软件运行所需要的sql语句简单一些的方法。

    下面是我自己的定制DatabaseHelper类。要使用这个类,你需要遵循一些指示。

    Here is my own customised DatabaseHelper class. To use this class you'll need to follow some instructions.

  • 如果SQLite数据库大小为1MB以上,然后将文件分割成块,我preFER 512KB块,并把它们放入/ RES / raw目录。
  • 编辑包名,并在下面的类你的数据库文件名。

  • If sqlite database size is more than 1MB then split the file into chunks , I prefer 512KB chunks and place them into /res/raw directory.
  • Edit the package name and your db file names in the following class. package your.packagee.name; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.content.res.Resources; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; public class DataBaseHelper extends SQLiteOpenHelper { private static final String pkg = "your package name"; private static String DB_PATH = "/data/data/" + pkg + "/databases/"; private static String DB_NAME = "yourDBFile.sqlite"; int[] dbfiles = { R.raw.chunk1 , R.raw.chunk2 ..... }; private SQLiteDatabase myDataBase; private final Context myContext; public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } public void createDataBase() { boolean dbExist = checkDataBase(); if (dbExist) { // do nothing - database already exist } else { this.getReadableDatabase(); try { CopyDataBase(); } catch (IOException e) { Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT) .show(); Log.d("Create DB", e.getMessage()); } } } private boolean checkDataBase() { SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); } catch (SQLiteException e) { Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT) .show(); Log.d("Check DB", e.getMessage()); } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } private void CopyDataBase() throws IOException { InputStream databaseInput = null; Resources resources = myContext.getResources(); String outFileName = DB_PATH + DB_NAME; OutputStream databaseOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[512]; int length; for (int i = 0; i < dbfiles.length; i++) { databaseInput = resources.openRawResource(dbfiles[i]); while ((length = databaseInput.read(buffer)) > 0) { databaseOutput.write(buffer, 0, length); databaseOutput.flush(); } databaseInput.close(); } databaseOutput.flush(); databaseOutput.close(); } public void openDataBase() throws SQLException { String myPath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); } @Override public synchronized void close() { if (myDataBase != null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public boolean deleteItem (String ID){ String query = "delete from item where id='" + ID + "'" ; Log.d("Query : ", query); try{ myDataBase.execSQL(query); return true ; } catch (Exception e){ Log.d("Exception", e.toString()); return false ; } } public Cursor getSearchFromID(String id) { return myDataBase.rawQuery("select * from item where id = \"" + id + "\"", null); } public boolean addSave(String type, String data , String time) { String query = "insert into item (type, data , timestamp) values ('" + type + "', '" + data + "', '" + time + "')"; try { myDataBase.execSQL(query); return true ; } catch (Exception e) { return false ; } } }

  • 下面是写为样本,如何使用它的一些方法。

    Here's some methods written as a sample , how to use it.

    用法很简单。当你的应用程序启动,这意味着在你的启动活动中使用这种code初始化数据库

    Usage is simple. When your application starts , that means in your Launcher activity use this code to initialize your database

    DataBaseHelper helper = new DataBaseHelper(this); helper.createDataBase(); helper.openDataBase(); helper.close();

    就用写在DatabaseHelper类中的方法。样本会是这样

    Then just use the methods written in DatabaseHelper class. A sample will be like this

    String id = "1"; DataBaseHelper helper = new DataBaseHelper(this); helper.openDataBase(); Cursor c = helper.getSearchFromID(id); if(c.getCount() > 0){ c.moveToFirst(); while(!c.isAfterLast()){ // extract your data from cursor c.MoveToNext(); } }

    希望这本书能解决你的所有关于Android的SQLite数据库的问题。至少它解决了我。谢谢你。

    Hope it will solve your all problems about sqlite database in Android. At least it solved for me. Thank you.

    更多推荐

    填充SQLite数据库

    本文发布于:2023-08-05 21:31:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1308171.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据库   SQLite

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!