I have tried following many ORMLite tutorials to use pre-existing SQLite database in android with ORMlite (like this ones 1,2,3) and followed guidance by this other SO post, but I still get:
java.sql.SQLException: SQL statement failed:
CREATE TABLE `countries` (`name` VARCHAR , `country_id` INTEGER PRIMARY KEY AUTOINCREMENT )
This is my DatabaseHelper
class:
/**
* DatabaseHelper which creates(copies in this case) and upgrades the database and provides the DAOs for the app
*/
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "countries.db"; //TODO ojo aca
private static final String DATABASE_PATH = "/data/data/com.cooervo.east_asiatouristattractions/databases/"; //TODO si concuerda el path
private static final int DATABASE_VERSION = 1;
private Dao<Country, String> countryDao;
public DatabaseHelper(Context context) {
super(context, DATABASE_PATH+DATABASE_NAME, null, DATABASE_VERSION);
boolean dbexist = checkdatabase();
if (!dbexist) {
// If database did not exist, try copying existing database from assets folder.
try {
File dir = new File(DATABASE_PATH);
dir.mkdirs();
InputStream input = context.getAssets().open(DATABASE_NAME);
String outFileName = DATABASE_PATH + DATABASE_NAME;
OutputStream output = new FileOutputStream(outFileName);
Log.i(DatabaseHelper.class.getName(), "DB Path : " + outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* Check whether or not database exist
*/
private boolean checkdatabase() {
boolean checkdb = false;
String path = DATABASE_PATH + DATABASE_NAME;
File dbfile = new File(path);
checkdb = dbfile.exists();
Log.i(DatabaseHelper.class.getName(), "DB Exist : " + checkdb);
return checkdb;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, Country.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, Country.class, true);
onCreate(sqLiteDatabase);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't upgrade/drop databases", e);
throw new RuntimeException(e);
}
}
public Dao<Country, String> getCountryDao () throws SQLException{
if(countryDao == null){
countryDao = getDao(Country.class);
}
return countryDao;
}
}
My countries.db seems to be in the correct assets folder:
This is my Country class which is the model for the countries table:
@DatabaseTable(tableName = "countries")
public class Country {
@DatabaseField(generatedId = true, columnName = "country_id")
private int id;
@DatabaseField(columnName = "name")
private String name;
public Country(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I used the following scripts to create and save countries.db
in sqlite3.exe, I used PRAGMA foreign_keys=on;
since I intend to include more tables with foreign keys:
PRAGMA FOREIGN_KEYS=ON;
CREATE TABLE `countries` (
`country_id` INTEGER NOT NULL,
`name` TEXT NOT NULL UNIQUE,
PRIMARY KEY(country_id)
);
INSERT INTO `countries` VALUES (1,'Japan'),
(2,'China');
Aucun commentaire:
Enregistrer un commentaire