当数据库已经存在时创建新表的问题

发布于 2024-12-14 08:19:31 字数 865 浏览 3 评论 0原文

我有一些奇怪的行为。 如果数据库不存在,我在活动中执行以下代码:

listOpenHelper = new ListOpenHelper(ManageListActivity.this);
db = listOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ListOpenHelper.TABLE_NAME, null, null, null, null, null, BaseColumns._ID + " DESC");

创建数据库并创建表 LIST,这里没有问题。

问题是当我尝试在其他活动中执行类似的块时:

productListOpenHelper = new ProductListOpenHelper(ProductListActivity.this);
db = productListOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ProductListOpenHelper.TABLE_NAME, null, null, null, null, null, ProductListOpenHelper.NAME + " ASC");

在这种情况下,我得到异常“android.database.sqlite.SQLiteException:没有这样的表:列表:,同时编译:SELECT * FROM list ORDER BY _id DESC “

如果我擦除数据库并首先执行上述块,并且在第一个块之后,错误将出现在productlist表中。

我需要在第一次执行时创建所有表? 我喜欢在用户输入每个活动时创建表,有什么好的方法可以做到这一点吗?

谢谢!

I'm having some strange behaviour.
If the database does not exists, and i execute the following code in my Activity:

listOpenHelper = new ListOpenHelper(ManageListActivity.this);
db = listOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ListOpenHelper.TABLE_NAME, null, null, null, null, null, BaseColumns._ID + " DESC");

The database is create and the table LIST is created, no problem here.

The problem is when i try to execute a similiar block in other Activity:

productListOpenHelper = new ProductListOpenHelper(ProductListActivity.this);
db = productListOpenHelper.getReadableDatabase();
Cursor cursor = db.query(ProductListOpenHelper.TABLE_NAME, null, null, null, null, null, ProductListOpenHelper.NAME + " ASC");

In this case, i get the Exception "android.database.sqlite.SQLiteException: no such table: list: , while compiling: SELECT * FROM list ORDER BY _id DESC"

If i erase the database and execute first the above block, and after the first block, the error will be in the productlist table.

I need to create all my tables in the first execution?
I like to create the tables when the user enter in each of the Activities, there is some good way to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰火雁神 2024-12-21 08:19:31

您有两个不同的数据库,对吗?如果不是,您可能不应该有两个不同的帮助器类。

此外,按照您的实际情况创建助手可能并不理想。请参阅博客文章:

http://www.touchlab.co/blog/single-sqlite-连接/

请发布您的帮助程序类的代码以及为什么您有两个不同的类。

You have two different databases, correct? If not, you probably shouldn't have two different helper classes.

Also, creating helpers as you are may not be ideal. See blog post:

http://www.touchlab.co/blog/single-sqlite-connection/

Please post the code for your helper classes and why you have two different classes.

幸福丶如此 2024-12-21 08:19:31

为什么你使用两个不同的助手?我认为这不是一个好方法...您可以在最初创建表,并且可以使用单个助手随时获取数据库。

why you are using two different helpers? i don't think its a good way... You can create the tables in the initially and you can obtain the db whenever you want, with a single helper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文