这是我的代码,它显示错误:
'no such table:AddressBookGroup:,While compiling select * from AddressBookGroup'
我已将db文件放在带有此表的asstes文件夹中。
我使用了此链接using-your-own-sqlite-database-in-android-applications
中的openhelper类代码import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class AddressBookGroup {
private final static String ID = "ID";
private final static String NAME = "Name";
public String Id;
public String Name;
private static final String DATABASE_PATH = "/data/data/com.devindia.dataSample/databases/";
private static final String DATABASE_NAME = "AirtelApp.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "AddressBookGroup";
private Context context;
private SQLiteDatabase db;
public AddressBookGroup(Context context) {
this.context = context;
OpenHelper oh=new OpenHelper(context);
try {
oh.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.db= oh.openDataBase();
int i=0;
}
public AddressBookGroup() {
// TODO Auto-generated constructor stub
}
public void insert(AddressBookGroup ret) {
final ContentValues values = new ContentValues();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
values.put(ID, cursor.getCount() + 1);
values.put(NAME, ret.Name);
db.insert(TABLE_NAME, null, values);
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public AddressBookGroup[] selectAll() {
// Cursor cursor = this.db.query(TABLE_NAME, new String[] { NAME },
// null,null, null, null, "name desc");
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
AddressBookGroup[] ret = new AddressBookGroup[cursor.getCount()];
if (cursor.moveToFirst()) {
int i = 0;
do {
ret[i] = new AddressBookGroup();
ret[i].Id = (cursor.getString(0));
ret[i].Name = (cursor.getString(1));
// ret[i].Name = cursor.getString(cursor.getColumnIndex(NAME));
// ret[i].Name= cursor.getString(cursor.getColumnIndex(PHONE));
i++;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return ret;
}
private class OpenHelper extends SQLiteOpenHelper {
Context context;
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created
// into the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DATABASE_PATH + DATABASE_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[3072];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public SQLiteDatabase openDataBase() throws SQLException {
SQLiteDatabase db1;
// Open the database
String myPath = DATABASE_PATH + DATABASE_NAME;
db1 = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
int i = 0;
return db1;
}
}
}
答案 0 :(得分:0)
首先在资产文件夹中复制数据库。并尝试实现以下代码。
public static void copyDataBase() { 尝试 { //打开本地数据库作为输入流
OutputStream databaseOutputStream = new
FileOutputStream("/data/data/com.devindia.dataSample/databases");
// Path to the just created empty db
InputStream databaseInputStream;
byte[] buffer = new byte[1024];
int length;
databaseInputStream = minnesota.databaseInputStream1;
while ( (length = databaseInputStream.read(buffer)) > 0 ) {
databaseOutputStream.write(buffer);
}
databaseInputStream.close();
databaseInputStream = minnesota.databaseInputStream2;
while ( (length = databaseInputStream.read(buffer)) > 0 ) {
databaseOutputStream.write(buffer);
}
databaseInputStream .close();
databaseOutputStream.flush();
databaseOutputStream.close();
}catch (Exception e) {
// TODO: handle exception
//Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}