未创建表 sqlite android

2024-02-23

我正在制作一个应用程序,我想在其中保存用户联系人详细信息。但是每当我尝试插入或选择某些值时,我都会收到错误。

数据库代码

public class ContactsDatabase extends SQLiteOpenHelper {

    private static final int dbVersion = 1;
    private static final String dbName = "HSsuraksha";
    private static final String tableName = "contactsDetails";
    private static final String contactId = "contactId";
    private static final String groupId = "groupId";
    private static final String groupGroupId = "groupId";
    private static final String groupTable = "groupDetails";
    private static final String contactName = "contactName";
    private static final String contactNumber = "contactNumber";
    private static final String createTable = "Create Table " + tableName + "(" + contactId + " Integer Primary Key AutoIncrement," + groupId + " Text," + contactName + " Text," + contactNumber + " Text" + ");";

    public ContactsDatabase(Context context) {
        super(context, dbName, null, dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(createTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

    }


    public void insertContacts(ContactModel contactModel, String id, ArrayList<ContactModel> contactModelArrayList) {
        SQLiteDatabase database = getWritableDatabase();
        database.beginTransaction();
        ContentValues contentValues = new ContentValues();
        for (int i = 0; i < contactModelArrayList.size(); i++) {

            contentValues.put(contactName, contactModelArrayList.get(i).getContactName());
            contentValues.put(contactNumber, contactModelArrayList.get(i).getContactNumber());
            contentValues.put(groupId, id);
            if (contentValues != null) {
                Long value = database.insert(tableName, id, contentValues);
            }

        }


        database.setTransactionSuccessful();
        database.close();
    }

    public void selectContacts(String id) {
        String query = "Select * From " + tableName + " where " + groupId + "=?";
        SQLiteDatabase database = getWritableDatabase();
        Cursor cursor = database.rawQuery(query, new String[]{id});
        while (cursor.moveToNext()) {
            cursor.getString(cursor.getColumnIndexOrThrow(contactName));
        }
        cursor.close();
        database.close();

    }
}

LOGCAT

08-01 18:53:57.820      672-672/example.com.pocketdocs E/SQLiteLog﹕ (1) no such table: contactsDetails
08-01 18:53:57.830      672-672/example.com.pocketdocs E/SQLiteDatabase﹕ Error inserting groupId=2 contactNumber=+91 97 69 512114 contactName=Aaaaaaa
    android.database.sqlite.SQLiteException: no such table: contactsDetails (code 1): , while compiling: INSERT INTO contactsDetails(groupId,contactNumber,contactName) VALUES (?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1475)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1347)
            at example.com.pocketdocs.DataBase.ContactsDatabase.insertContacts(ContactsDatabase.java:54)
            at example.com.pocketdocs.Group.CreateNewGroup.onClick(CreateNewGroup.java:104)
            at android.view.View.performClick(View.java:4147)
            at android.view.View$PerformClick.run(View.java:17161)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

我尝试解决该错误但没有成功。我的代码有什么问题吗???


我有另一个具有相同数据库名称的表 groupInfo,所以这是问题吗?

这是一个问题。发生的情况如下:

  • 访问具有相同数据库文件的第一个 sqlite 打开助手。如果数据库文件不存在,则onCreate()调用回调以便您可以设置数据库文件。

  • 访问具有相同数据库文件的另一个 sqlite 打开助手。具有给定名称的数据库文件已存在且版本正确,因此不存在onCreate() or onUpgrade()被调用。相反,文件只是被打开。

解决方案:每个数据库文件仅使用一个 sqlite 打开帮助程序。将两个表的创建语句放在同一个助手中onCreate() method.

同时卸载您的应用程序,以便删除仅包含另一个表的旧数据库文件。

请参阅链接的问题SQLiteOpenHelper onCreate() / onUpgrade() 何时运行? https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run了解有关 sqlite open helper 生命周期回调的更多信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未创建表 sqlite android 的相关文章

随机推荐

  • MPlayer 如何识别 MJPEG 流?

    由于 MJPEG over http 基本上包括传输由定义的分隔符分隔的一系列 JPEG 图像 MPlayer 如何识别它是 MJPEG 流 谢谢 看一下 MplayerMjpegStreamViewing http www lavrsen
  • web.config 反斜杠出现两次 Visual Studio 2010

    我有一个在 Visual Studio 2008 中制作的 asp net Web 应用程序 一切都工作得很好 直到我切换到 VS 2010 当发生这种情况时 我开始看到数据库连接字符串出现一些奇怪的行为 该字符串 已编辑 但格式相同 如下
  • x86-64 中 movq 和 movabsq 的区别

    我说的是 x86 64 Intel 架构中的数据移动指令 我读过 常规的movq指令只能具有可表示为 32 位二进制补码数的立即源操作数 而movabsq指令可以将任意 64 位立即数作为其源操作数 并且只能将寄存器作为目标 您能详细说明一
  • Docker-compose:如何使用相同的网络地址从容器和主机访问 Localstack 资源

    我使用 docker compose 在 2 个独立的容器中运行 python API 和 Localstack 实例 以进行本地开发 该 API 有一个端点 可生成预签名的 AWS S3 URL 并重定向用户 以便直接从 S3 加载图像
  • 如何从私有模块中的公共函数引用私有类型?

    我想将类似的函数分组在一个文件中 这些函数需要返回一个非公开的类型 struct MyStruct mod my mod use super MyStruct There are other similar functions that a
  • 通过 INavigationProvider 将菜单项添加到主菜单但不显示?

    使用 Orchard cms 1 5 1 我创建了一个模块 其中包含从 Web 服务获取列表的控制器 我想在启用此模块时在主菜单中添加一个菜单项 为此我创建了 主菜单如下 public class MainMenu INavigationP
  • 如何获取 ESRI 地图 - Angular 的屏幕截图

    如何在 UI 上获取 Esri 地图当前状态的屏幕截图并将其从 Angular 下载为 Pdf 以下代码是我当前的 ts 代码 如果需要添加其他部分 请告诉我 esri map component html div div esri map
  • Javascript crc32 函数和 PHP crc32 不匹配

    我正在开发一个 web 应用程序 我想将 PHP 中服务器端生成的一些 crc32 值与我在 Javascript 中生成的一些 crc32 值进行匹配 两者都使用相同的输入字符串但返回不同的值 我在webtoolkit上找到了一个crc3
  • Python:有吸引力的、干净的、可打包的 Windows GUI 库 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我需要为最终用户下载的桌面应用程序创建一个简单的基于 Windows 的 GUI 该应用程序是用 python 编写的 并将打包为安装程序
  • 在c中读取/写入bmp文件

    我正在尝试处理 bmp 文件 首先 我尝试从 bmp 文件读取标题和数据并将其写入新文件 pragma pack push 1 Windows 3 x bitmap file header typedef struct char filet
  • golang http2 服务器推送的高级客户端

    golang 1 6 发布 支持 Http2 我在网上搜索 但找不到任何有关如何使用 Go 进行 Http2 服务器推送的示例 是否有为此实现的高级客户端 人们已经做过什么例子了吗 std 库中的 http2 实现不公开 http2 特定的
  • AngularJS 和 i18next

    我见过一些 Angular 的 i18n 插件 但我不想重新发明轮子 i18next 是一个很好的库 所以我打算使用它 我创建了一个指令 i18n 它只调用 i18n 库 define app jquery i18n function ap
  • 按年份划分的R组

    我将 csv 读入 R 现在我有了一个数据列表 head data Date Open High Low Close Volume 1 31 Dec 14 223 09 225 68 222 25 222 41 2402097 2 30 D
  • Android - 使用动画的工件

    应用发现的 3D 转换时 我遇到屏幕上的视觉伪影问题here http developer android com resources samples ApiDemos src com example android apis animat
  • d3.js - 变换和过渡,多行

    我已按照以下说明进行操作 http bost ocks org mike path http bost ocks org mike path 用于用单线创建单图并对其进行动画处理 并且 弄清楚如何在图表中创建多条线 在 D3 js 中绘制多
  • 代码以交互方式运行时显示结果,但从 shell 运行时不显示结果

    我从另一张海报上借用了这个科学记数法小脚本 以科学记数法显示小数 https stackoverflow com questions 6913532 python how to convert decimal to scientific n
  • 音频 API 示例中的“安全错误”代码:“1000”

    我试图重复这个例子here https wiki mozilla org Audio Data API Complete Example 3a Visualizing Audio Spectrum 但收到此错误 Security error
  • C# 中的 DataGridView 组合框单元格事件

    我想在 DataGridViewComboBox 中的项目发生更改时显示一条消息 我可以通过 datagridview CellbeginEdit 事件和 CellEndEdit 事件的帮助来部分执行它 但这还不够 我希望它发生在组合框选择
  • 如何在ios应用程序中播放RTMP视频流?

    嗨 我正在开发广播应用程序 我正在使用 Videocore 库 现在我如何在 ios 应用程序中播放该流视频 我尝试使用 MpMoviePlayer 但它不支持 rtmp 流 那么是否有任何第三方库可供 RTMP 支持的播放器使用 请帮助我
  • 未创建表 sqlite android

    我正在制作一个应用程序 我想在其中保存用户联系人详细信息 但是每当我尝试插入或选择某些值时 我都会收到错误 数据库代码 public class ContactsDatabase extends SQLiteOpenHelper priva