1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| package com.importdatabase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
//db = getWritableDatabase();
db = getReadableDatabase();
}
//nom de la base
private static String DB_NAME = "ELECT.sqlite";
// le context
private final Context myContext = null;
private static final int DATABASE_VERSION = 4;
private SQLiteDatabase db;
//private static String TABLE_LISTELEC = "LISTELEC1";
/**
* Constructeur
*
* @param context
*/
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (!dbExist)
{
db_Read = this.getReadableDatabase();
db_Read.close();
copyDBFromResource();
File bddfile = myContext.getDatabasePath(DB_NAME);
String database_file = bddfile.getPath();
SQLiteDatabase dbe = null;
try
{
dbe = SQLiteDatabase.openDatabase(database_file, null, SQLiteDatabase.OPEN_READWRITE);
dbe.setLocale(Locale.getDefault());
dbe.setLockingEnabled(true);
dbe.setVersion(1);
dbe.close();
}
catch(SQLiteException e)
{
}
}
}
//-----------------------------------------------------------------------------------
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
File bddfile = myContext.getDatabasePath(DB_NAME);
String database_file = bddfile.getPath();
checkDB = SQLiteDatabase.openDatabase(database_file, null, SQLiteDatabase.OPEN_READONLY);
}
catch(SQLiteException e)
{
//database does't exist yet.
}
if (checkDB != null) checkDB.close();
return checkDB != null ? true : false;
}
//-----------------------------------------------------------------------------------
public void openDataBase() throws SQLException
{
//open the database
File bddfile = myContext.getDatabasePath(DB_NAME);
String database_file = bddfile.getPath();
db = SQLiteDatabase.openDatabase(database_file, null, SQLiteDatabase.OPEN_READWRITE);
}
//-----------------------------------------------------------------------------------
@Override
public void onCreate(SQLiteDatabase db)
{
}
//-----------------------------------------------------------------------------------
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
//-----------------------------------------------------------------------------------
private void copyDBFromResource()
{
File fileTest = myContext.getDatabasePath(DB_NAME);
// Open the empty db as the output stream
try
{
InputStream databaseInputStream;
OutputStream databaseOutputStream;
byte[] buffer;
int length;
databaseInputStream = myContext.getAssets().open(DB_NAME);
databaseOutputStream = new FileOutputStream(fileTest);
buffer = new byte[1024];
length = 0;
while ((length = databaseInputStream.read(buffer)) != -1)
databaseOutputStream.write(buffer, 0, length);
//close the streams
databaseInputStream.close();
databaseOutputStream.flush();
databaseOutputStream.close();
}
catch (FileNotFoundException e)
{
throw new Error("File not found.");
}
catch (IOException e)
{
throw new Error("Problem copying database from resource file.");
}
} } |
Partager