检测一个表包含列的Andr​​oid /源码源码、Andr、oid

2023-09-07 12:01:15 作者:蓅篂劃濄の慯

所以,我有在市场上的应用程序,并通过更新我想一些列添加到数据库中。没有问题为止。但我想,以检测是否使用的数据库缺少这些列,并加入他们,如果是这种情况。我需要这个是刚刚更新到新版本后,动态和不这样做,因为应用程序应该仍然能够导入旧数据库。通常,我将能够使用 PRAGMA 的查询,但是我不知道如何与Android的做到这一点。我不能用execSQL,因为它是一个查询,我无法弄清楚如何使用PRAGMA与查询() - 函数

So I have an app on the market, and with an update I want to add some columns to the database. No problems so far. But I want to detect if the database in use is missing these columns, and add them if this is the case. I need this to be done dynamically and not just after the update to the new version, because the application is supposed to still be able to import older databases. Normally I would be able to use the PRAGMA query, but Im not sure how to do this with Android. I cant use execSQL since it is a query, and I cant figure out how to use PRAGMA with the query()-function.

Ofcourse我可以捕捉异常,然后添加列,或总是将列添加到每个表之前,我先从它的工作,但是这不是一个巧妙的解决办法。

Ofcourse I could just catch exceptions and then add the column, or always add the columns to each table before I start to work with it, but that is not a neat solution.

干杯,

推荐答案

杂注工作。作为一个例子,我刚在我的Andr​​oid应用程序的DB之一。

Pragma works. As an example I just tried it on one of my Android App DBs.

sqlite> pragma table_info (userCommand);

cid name        type    notnull     dflt_value  pk
0   id      INTEGER     0       1
1   description TEXT    0       0
2   message     TEXT    0       0

我们可以看到有指定表中的三个字段。标识,描述,和消息

As we can see there are three fields in the specified table. id, description, and message.

因此​​,在你的应用程序,使用类似:

So in your app, use something like:

Cursor c = null;
c = RawQuery ("pragma table_info (userCommand)",null); 
// if c has records and is not null then iterate through the records in search of your table name. 

请原谅可怜的格式。

 
精彩推荐