我在使用三星设备时遇到了麻烦。在我的应用程序中,每次我在市场上传新版本,我想要替换数据库...但在我尝试保存一些数据之前。该代码在ADV和许多设备上都能正常工作,但三星Galaxy(S系列,标签)等设备除外。
我已经尝试过this.getReadableDatabase()到this.getWritableDatabase(),但没有任何改变。
我还检查过类似的帖子Failed to create SQLite DB in my Samsung y duos device in Android?
有人可以帮帮我吗?以下是基本代码和我的尝试...
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db = null;
if(dbExist){
}else{
db = this.getReadableDatabase();
db.close();
try {
copyDataBase();
Log.e(TAG, "createDatabase database created");
} catch (IOException e) {
throw new Error("Erro ao copiar banco");
}
}
}
/*
Both file or a database check works
*/
private boolean checkDataBase(){
File checkDB = new File (Bd_path + Bd_name);
return checkDB.exists();
/*SQLiteDatabase checkDB = null;
try {
String myPath = Bd_path + Bd_name;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;*/
}
...
首先尝试 - 除三星设备外正常工作
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db = null;
if(dbExist){
try {
openDataBase();
Cursor resul = bancoDados.rawQuery("Select ativo from configuracao", null);
resul.moveToFirst();
if (Integer.valueOf(resul.getString(resul.getColumnIndex("ativo"))) <= 1) {
copyDataBase();
Log.v("Data", "updated");
bancoDados.execSQL("UPDATE configuracao SET ativo = '2'");
}
resul.close();
bancoDados.close();
}catch (Exception e) {
e.printStackTrace();
}
...
第二次尝试 - 除三星设备外正常工作
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
SQLiteDatabase db = null;
if(dbExist){
try {
db = this.getReadableDatabase();
Cursor resul = db.rawQuery("Select ativo from configuracao", null);
resul.moveToFirst();
if (Integer.valueOf(resul.getString(resul.getColumnIndex("ativo"))) <= 1) {
copyDataBase();
Log.v("Data", "updated");
db.execSQL("UPDATE configuracao SET ativo = '2'");
}
resul.close();
db.close();
}catch (Exception e) {
e.printStackTrace();
}
...
当设备尝试读取我的数据库时出现了流动错误
android.database.sqlite.SQLiteException: no such table: configuracao: , while compiling: Select ativo from configuracao
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:110)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:71)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
at com.ead.louvemosfinal.Dados_banco.createDataBase(Dados_banco.java:47)
at com.ead.louvemosfinal.DataAdapter.createDatabase(DataAdapter.java:31)
at com.ead.louvemosfinal.Inicio.construtor(Inicio.java:116)
at com.ead.louvemosfinal.Inicio.onCreateView(Inicio.java:97)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:279)
at android.view.View.dispatchAttachedToWindow(View.java:6177)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1266)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1271)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1271)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1271)
at android.view.ViewRoot.performTraversals(ViewRoot.java:771)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1868)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3709)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
当它读取数据库时,无法在以下类(Inicio.class)上加载此表中的数据
Code of Inicio.class
...
bancoDados = new DataAdapter(getActivity());
bancoDados.createDatabase();
bancoDados.open();
settings = bancoDados.carregaConfiguracao()
bancoDados.close();
...
我得到的错误
android.database.sqlite.SQLiteException: no such table: configuracao: , while compiling: SELECT * FROM configuracao
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1357)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1325)
at com.ead.louvemosfinal.DataAdapter.carregaConfiguracao(DataAdapter.java:429)
at com.ead.louvemosfinal.Inicio.construtor(Inicio.java:118)
at com.ead.louvemosfinal.Inicio.onCreateView(Inicio.java:97)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:279)
at android.view.View.dispatchAttachedToWindow(View.java:6156)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1122)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1127)
at android.view.ViewRoot.performTraversals(ViewRoot.java:773)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
来自DataAdapter的carregaConfiguracao代码
public String[] carregaConfiguracao(){
Cursor resul = mDb.rawQuery("SELECT * FROM configuracao", null);
String[] dados = new String[11];
resul.moveToFirst();
dados[0] = resul.getString(resul.getColumnIndex("_id"));
dados[1] = resul.getString(resul.getColumnIndex("fonte"));
dados[2] = resul.getString(resul.getColumnIndex("nome"));
dados[3] = resul.getString(resul.getColumnIndex("email"));
dados[4] = resul.getString(resul.getColumnIndex("cor_nota"));
dados[5] = resul.getString(resul.getColumnIndex("cor_letra"));
dados[6] = resul.getString(resul.getColumnIndex("contraste"));
dados[7] = resul.getString(resul.getColumnIndex("refrao"));
dados[8] = resul.getString(resul.getColumnIndex("acordes"));
dados[9] = resul.getString(resul.getColumnIndex("musica"));
dados[10] = resul.getString(resul.getColumnIndex("cantor"));
resul.close();
return dados;
}
答案 0 :(得分:0)
使用这个DataBaseHandler Calss.it可以解决所有的android问题。
import java.util.ArrayList;
import java.util.List;
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.SQLiteOpenHelper;
public class DatabaseHandler {
// Database
private static final String DATABASE_NAME = "Heat";
private static final int DATABASE_VERSION = 1;
// field
private static final String KEY_ID = "id";
private static final String KEY_TAGS_COLOR = "tag_color";
private static final String KEY_TAGS_TAG_ID = "tag_id";
private static final String KEY_TAGS_DESCRIPTION = "tag_description";
private static final String KEY_TAGS_FLAG_ACTIVE = "isActive";
// TABLES
private static final String TABLE_NAME_USER_DETAIL = "user_details";
private static final String TABLE_NAME_TAGS = "tags";
// CREATE TABLES
private static final String CREATE_TAGS_TABLE = "create table "
+ TABLE_NAME_TAGS + " ( " + KEY_ID
+ " integer primary key autoincrement," + KEY_TAGS_TAG_ID
+ " integer," + KEY_TAGS_DESCRIPTION + " text ," + KEY_TAGS_COLOR
+ " text ," + KEY_TAGS_FLAG_ACTIVE + " text ); ";
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
private static final String TAG = "DatabaseHandler";
public DatabaseHandler(Context context) {
dbHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TAGS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
public DatabaseHandler open() throws SQLException {
db = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public void insertTagList(List<YourModelCalss> tag_list) {
if (tag_list == null) {
return;
}
for (YourModelCalss tags : tag_list) {
insertTag(tags);
}
}
private void insertTag(YourModelCalss tags) {
// AppLog.Log(TAG, "Inserting into Database TAG :: " + tags.getDesc());
ContentValues values = new ContentValues();
values.put(KEY_TAGS_TAG_ID, tags.getId());
values.put(KEY_TAGS_DESCRIPTION, tags.getDesc());
values.put(KEY_TAGS_COLOR, tags.getColor());
int active_flag = 0;
if (tags.getFlag_active() != null
&& tags.getFlag_active().equalsIgnoreCase("1")) {
active_flag = 1;
}
values.put(KEY_TAGS_FLAG_ACTIVE, active_flag);
try {
open();
db.insert(TABLE_NAME_TAGS, null, values);
} catch (Exception e) {
e.printStackTrace();
}
close();
}
public ArrayList<YourModelCalss> getTagList() {
ArrayList<YourModelCalss> tagList = new ArrayList<YourModelCalss>();
open();
Cursor cursor = db.query(TABLE_NAME_TAGS, new String[] {
KEY_TAGS_TAG_ID, KEY_TAGS_DESCRIPTION, KEY_TAGS_COLOR,
KEY_TAGS_FLAG_ACTIVE }, null, null, null, null, null);
if (cursor != null && cursor.getCount() == 0) {
} else {
cursor.moveToFirst();
YourModelCalss tag;
do {
tag = new YourModelCalss();
tag.setId(cursor.getInt(0));
tag.setDesc(cursor.getString(1));
tag.setColor(cursor.getString(2));
tag.setFlag_active(cursor.getString(3));
tagList.add(tag);
} while (cursor.moveToNext());
}
cursor.close();
cursor = null;
close();
return tagList;
}
public void clearTagData() {
try {
open();
db.delete(TABLE_NAME_TAGS, null, null);
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
}
}