引起:android.database.sqlite.SQLiteException:没有这样的表:(代码1)Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24634116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:32:01  来源:igfitidea点击:

Caused by: android.database.sqlite.SQLiteException: no such table: (code 1) Android

androidandroid-sqlite

提问by Sniper

We have a sqlite database in our Application. Its working fine for all the users but few of them experiencing the Caused by: android.database.sqlite.SQLiteException: no such table: generalSettings (code 1): , while compiling: select * from generalSettingserror.

我们的应用程序中有一个 sqlite 数据库。它适用于所有用户,但很少有人遇到Caused by: android.database.sqlite.SQLiteException: no such table: generalSettings (code 1): , while compiling: select * from generalSettings错误。

Below is my sqlite helper class to create the db and the error log. In assert/Master.dbwe have the table generalSettings. But after copying it to the device the table is missing. This is happening only for few users. I searched for the solution but I cant find the exact one. Team please help me to fix this.

下面是我用于创建数据库和错误日志的 sqlite 助手类。在assert/Master.db我们有桌子 generalSettings。但是在将其复制到设备后,该表丢失了。这仅发生在少数用户身上。我搜索了解决方案,但找不到确切的解决方案。团队请帮我解决这个问题。

Code:

代码:

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class InstallDB extends SQLiteOpenHelper {
    Context ctx;

    String DBNAME;
    String DBPATH;
    Modules modObj = new Modules();

    public InstallDB(Context context, String name) {
        super(context, name, null, 1);
        this.ctx = context;
        this.DBNAME = name;

        this.DBPATH = this.ctx.getDatabasePath(DBNAME).getAbsolutePath();
        Log.e("Path 1", DBPATH);

    }

    public void createDataBase() {

        boolean dbExist = checkDataBase();

        SQLiteDatabase db_Read = null;

        if (!dbExist) {
            synchronized (this) {

                db_Read = this.getReadableDatabase();
                Log.e("Path 2", this.getReadableDatabase().getPath());
                db_Read.close();

                copyDataBase();
                Log.v("copyDataBase---", "Successfully");
            }

            // try {

            // } catch (IOException e) {
            // throw new Error("Error copying database");
            // }
        }
    }

    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DBPATH;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        } catch (Exception e) {
            Log.i("SQLite Error", "database does't exist yet.");
        }

        if (checkDB != null) {
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }

    private void copyDataBase() {

        try {
            InputStream myInput = ctx.getAssets().open(DBNAME);
            String outFileName = DBPATH;

            OutputStream myOutput = new FileOutputStream(outFileName);

            byte[] buffer = new byte[1024 * 3];

            int length = 0;

            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
            Modules.stacTaceElement = e.getStackTrace();

            StringWriter stackTrace1 = new StringWriter();
            e.printStackTrace(new PrintWriter(stackTrace1));
            System.err.println(stackTrace1);

            Intent send = new Intent(Intent.ACTION_SENDTO);
            String uriText;

            uriText = "mailto:[email protected]"
                    + "&subject=Error Report"
                    + "&body="
                    + stackTrace1.toString();

            uriText = uriText.replace(" ", "%20");
            Uri uri = Uri.parse(uriText);

            send.setData(uri);
            ctx.startActivity(Intent.createChooser(send, "Send mail..."));
            // TODO: handle exception
        }

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

Error Log:

错误日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{palmagent.FidelityAgent.Two/palmagent.FidelityAgent.Two.PassNew}: android.database.sqlite.SQLiteException: no such table: generalSettings (code 1): , while compiling: select * from generalSettings
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.access0(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: generalSettings (code 1): , while compiling: select * from generalSettings
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at palmagent.FidelityAgent.Two.masterDatabase.selectquery(masterDatabase.java:59)
at palmagent.FidelityAgent.Two.Modules.checkDatabase(Modules.java:28825)
at palmagent.FidelityAgent.Two.PassNew$LoaduserDetails.onPreExecute(PassNew.java:140)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at palmagent.FidelityAgent.Two.PassNew.onCreate(PassNew.java:120)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
... 11 more

回答by Ratul Ghosh

The problem is because some of device is upgrading your app, so the checkDataBase()returning true, so you are not calling copyDataBase(). So you are using previous database which doesn't have generalSettingstable. To solve this try:

问题是因为某些设备正在升级您的应用程序,因此checkDataBase()返回true,因此您没有调用copyDataBase(). 所以你使用的是以前没有generalSettings表的数据库。要解决此问题,请尝试:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
  copyDatabase();
}

and also update your constructor:

并更新您的构造函数:

public InstallDB(Context context, String name) {
    super(context, name, null, DB_VERSION); 
    // DB_VERSION is an int,update it every new build

    this.ctx = context;
    this.DBNAME = name;
    this.DBPATH = this.ctx.getDatabasePath(DBNAME).getAbsolutePath();
    Log.e("Path 1", DBPATH);

}

回答by Babul Mirdha

After spending couple of hours I got this solution:

花了几个小时后,我得到了这个解决方案:

1) Settings > Application Manager

1) 设置 > 应用程序管理器

2) Select App

2) 选择应用

3) Clear Data

3) 清除数据

4) Uninstall App

4) 卸载应用

Now Run app from Android Studio

现在从 Android Studio 运行应用程序

Hope this works properly

希望这能正常工作

回答by Mehdi1991

This error Occurs Because you are not using DATABASE_VERSION

发生此错误是因为您没有使用 DATABASE_VERSION

public class DatabaseHelper extends SQLiteOpenHelper {
    private static SQLiteDatabase sqliteDb;

    private static DatabaseHelper instance;

    private static final int DATABASE_VERSION = 1;

Increase Your version every time you make changes in your database just increase DATABASE_VERSION with +1..

每次在数据库中进行更改时都增加您的版本,只需将 DATABASE_VERSION 增加 +1..

回答by Developer

Another possible solution is just uninstall an App from the Android emulator and after this run it again.

另一种可能的解决方案是从 Android 模拟器卸载应用程序,然后再次运行它。

If you just want to remove a application:

如果您只想删除应用程序:

1.Start the emulator.
2.Open the Android settings app.
3.Select "Applications" (Called "Apps" on Android 4.0 or higher)
4.Select "Manage Applications" (Only on Android 3.2 or lower)
5.Select the application you want to uninstall.
6.Click "Uninstall"
1.Start the emulator.
2.Open the Android settings app.
3.Select "Applications" (Called "Apps" on Android 4.0 or higher)
4.Select "Manage Applications" (Only on Android 3.2 or lower)
5.Select the application you want to uninstall.
6.Click "Uninstall"

回答by Blasanka

Even if you have coded for few tables and upgraded database, your app will crash. What you have to do is for each and every table create database changes upgrade the DATABASE_VERSION

即使您为几个表编码并升级了数据库,您的应用程序也会崩溃。你要做的是为每一个表创建数据库更改升级DATABASE_VERSION

If you have few DatabaseHelper classes(read this), you have to add this method to your db helper:

如果您的 DatabaseHelper 类很少(阅读本文),则必须将此方法添加到您的数据库助手中:

@Override
public void onOpen(SQLiteDatabase db) {
    onCreate(db);
}

回答by Shubham Raitka

Suppose if you run your app with Database Version 1, then if you alter the table structure or add a new table, you must increase your Database Version to 2 and further if you make more changes to it.

假设您使用数据库版本 1 运行您的应用程序,那么如果您更改表结构或添加新表,您必须将数据库版本增加到 2,如果您对其进行更多更改,则必须进一步增加。

public class AppDatabase extends SQLiteOpenHelper {

    // Database Name
    private static final String DATABASE_NAME = "myDatabase";

    // Database Version
    private static final int DATABASE_VERSION = 1;    

    public AppDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
}

Increase this DATABASE_VERSION value if any alterations are done.

如果进行了任何更改,请增加此 DATABASE_VERSION 值。

回答by Catluc

If you are using GreenDao and get this error, be sure you are unistalling the app and try again. This solved my problem

如果您正在使用 GreenDao 并收到此错误,请确保您正在卸载应用程序并重试。这解决了我的问题

回答by Md.Tarikul Islam

1.Change Your DataBase Version OrFirst Uninstall Your Apps In the Emulator or Phone And Re-install. I In This way Think Your Problem Will be solved.

1.更改您的数据库版本 首先在模拟器或手机中卸载您的应用程序并重新安装。我以这种方式认为您的问题将得到解决。

回答by Hideya

it's an upgrading exception. Make sure you have the table in your previous database. If not, create it. PS: if you're newly dev this app, uninstall it from your emulator or your device and re-install. But its not recommended for the data will ne lost.

这是一个升级例外。确保您在以前的数据库中有该表。如果没有,请创建它。PS:如果您是新开发此应用程序,请从您的模拟器或设备中卸载它并重新安装。但不建议这样做,因为数据会丢失。

回答by Rashed Zzaman

In my case I changed my db name and its work for me

就我而言,我更改了数据库名称及其对我的工作