Greendao operation of Local DB files (using Greendao new 3.2.2)

Source: Internet
Author: User
Tags sqlite

Project requirements: Provinces and cities using local DB files, the database uses the Greendao framework. Now do not want to use SQL statements, with Greendao directly query the local database table (as for why use Greendao, you can Baidu it advantage).

Idea: 1. Copy the DB file to the default directory of the database

        /data/data/your project package name/databases/table name (same as Greendao generated table)

2. Greendao generates the entity, DAO file for the corresponding table, and uses the Greendao encapsulated method to manipulate the copied database table.

Problems encountered:

1, android.database.sqlite.SQLiteException:table ' tb_city ' already exists (code 1):,

Read the newspaper wrong say table tb_city already exists, create it again the Times database exception.

Cause: You can view the generated daomaster,createalltables (Db,false). Second parameter, default = False (CREATE TABLE)

    

Workaround: Use greendao3.2.2 to generate the entity using annotations @entity (createindb=false)//createindb whether to create the table, by default true. As in this project, the table has been copied and does not need to be created, set to false.

Daomaster in createalltable (db, parameter), parameters are generated by default and cannot be changed.

2, Android.database.sqlite.SQLiteException:no such column:t.province_id (code 1):,

          Cause: The field name does not correspond to the field in the DB file when the entity is created.
Workaround: Check the Modified field, type.

Project Use Process: (greendao3.2 of the use of the process online there are many, according to the steps can be done).


Database Copy method:
       
private void Copydbtodatabases () {
String db_path= "/data/data/package name/databases/";
try {
String outfilename = Db_path + db_name;
File File = new file (Db_path);
if (!file.mkdirs ()) {
File.mkdirs ();
}
File datafile = new file (outfilename);
if (datafile.exists ()) {
Datafile.delete ();
}
InputStream Myinput;
Myinput = This.getassets (). open (db_name);
OutputStream myoutput = new FileOutputStream (outfilename);
byte[] buffer = new byte[1024];
int length;
while (length = myinput.read (buffer)) > 0) {
Myoutput.write (buffer, 0, length);
}
Myoutput.flush ();
Myoutput.close ();
Myinput.close ();
LOG.D ("Haifeng", "Copy db");
} catch (IOException e) {
LOG.D ("Haifeng", "error" + e.tostring ());
E.printstacktrace ();
}
}
DAO Management class:
/**
* Created by Administrator on 2017/6/5.
*/

public class Greendaomanager {
Private Daomaster Mdaomaster;
Private Daosession mdaosession;
private static Greendaomanager minstance;
Single case
public static Greendaomanager getinstance () {
if (minstance==null) {
Guaranteed asynchronous handling of security operations
Synchronized (Greendaomanager.class) {
if (minstance==null) {
Minstance=new Greendaomanager ();
}
}
}
return minstance;
}

Private Greendaomanager () {
if (minstance==null) {
Daomaster.devopenhelper openhelper=new Daomaster.devopenhelper (App.getinstance (), App.getInstance (). Db_name,null);
Mdaomaster=new Daomaster (Openhelper.getwritabledatabase ());
Mdaosession=mdaomaster.newsession ();
}
}

Public Daomaster Getmaster () {
return mdaomaster;
}
Public Daosession getsession () {
return mdaosession;
}
Public Daosession getnewsession () {
Mdaosession=mdaomaster.newsession ();
return mdaosession;
}
}
Call:
Table to query Province
Tb_provincedao provinceentitydao= greendaomanager.getinstance (). GetSession (). Gettb_provincedao ();
List<tb_province> list= Provinceentitydao.loadall ();
if (List.size () >0) {
for (int i = 0; i < 3; i++) {
Toast.maketext (App.getinstance (),
"Province ID:" + list.get (i). Getprovinceid ()
+ "; Province name:" + list.get (i). Getprovincename ()
+ "; City ID:" + list.get (i). Getcountryid ()
, Toast.length_short). Show ();
}
}





Greendao operation of Local DB files (using Greendao new 3.2.2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.