SQLiteOpenHelper 问题
SQLiteOpenHelper problems
我一直在使用 Android 和 SQLite 数据库开发应用程序。
但我有一些问题。
这是我的代码:
SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
SQLiteDatabase database = SQLiteHelperMoney1.getReadableDatabase();
Cursor cursorCategory = database.query(false, "CATEGORY", null, null, null, null, null, null, null);
SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, null, null, 1);
SQLiteDatabase database2 = SQLiteHelperMoney2.getReadableDatabase();
Cursor cursorTransaction = database2.query(false, "TRANSACTIONS", null, null, null, null, null, null, "10");
现在的问题是,在我的游标 cursorCategory
中,我得到了存储在我数据库的 Category
table 中的数据。但是我在cursorTransaction
中没有得到任何数据,为什么会这样??
此外,在实例化我的 SQLiteHelperMoney2
对象时,如果我将 SQLiteHelperMoney2
对象的实例化更改为此:
SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, "TRANSACTIONS", null, 1);
然后我从 Transaction
table 获取数据,在我的 cursorTransaction
.
但是为什么会这样呢??根据文档,在 SQLiteOpenHelper 的构造函数中,第二个参数是数据库的名称,而不是 table 的名称。
那么为什么在字段名称数据库中给出 TABLE 的名称会给出正确答案?
此外,文档说在 SQLiteOpenHelper 的构造函数中,如果数据库已经在内存中,您可以在字段 "NAME" 中指定 null
。我假设数据库是在应用程序本身中创建的,所以它只会在内存中,这就是为什么我在这里将第二个参数指定为 null :
SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
但它在这里有效,但在其他情况下无效。
另外,默认创建的数据库名称是什么?因为我没有在 SQLiteOpenHelper 的 onCreate
函数中指定它。我可以更改我的数据库的名称吗??
编辑:
onCreate
方法:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE CATEGORY (_ID INTEGER PRIMARY KEY, NAME TEXT)");
db.execSQL("CREATE TABLE TRANSACTIONS (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, CATEGORY TEXT, NOTE TEXT, TIME TEXT, EXPENSE INTEGER)");
db.execSQL("CREATE TABLE BUDGET (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, TIME TEXT)");
ContentValues cv = new ContentValues();
cv.put("NAME", "FOOD");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "TRAVEL");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "INCOME");/*INCOME*/
db.insert("CATEGORY", null, cv);
cv.put("NAME", "TRANSPORTATION");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "MOVIE");
db.insert("CATEGORY", null, cv);
}
@laalto:我在这里阅读了您的回答:When is SQLiteOpenHelper onCreate() / onUpgrade() run?
而且我对磁盘上和内存中的数据库有点困惑。在我的 onCreate
方法中,我在 Category
table 中添加了行,因为我相信 onCreate 方法在应用程序的生命周期中只被调用一次,即当它是 运行 第一次,用于创建数据库。
这就是为什么在我的 onCreate
方法中我将数据添加到我的 CATEGORY
table,因为我希望它有一些初始数据。
而且我相信当我们对 getReadableDatabase()
和 getWritableDatabase()
进行替代调用时,它 returns 为我们提供了先前创建的数据库的副本,或者可能是一个句柄原始数据库并在完成时保存这些更改。但是,在阅读您的回答后,似乎 onCreate
方法每次都是 运行。如果每次都是 运行 数据存储在哪里,因为我们关闭数据库时会丢失数据库??
Now the problem is, that in my cursor cursorCategory I get the data that is stored in the Category table of my database. But I do not get any data in the cursorTransaction, why does this happen??
查询returns您刚创建的内存数据库中没有数据。
Furthermore, while instantiating my SQLiteHelperMoney2 object, if I change the instantiation of my SQLiteHelperMoney2 object to this :
...
But why does this happen??
查询 returns 先前创建的磁盘数据库中的一些数据。
出于某种巧合,数据库文件的名称与 table 相同。卸载并重新安装您的应用程序,或在应用程序管理器中清除其数据以删除可能仍然存在的旧数据库文件。
Furthermore, the documentation says that in the constructor for SQLiteOpenHelper, in the field "NAME" you can specify null if the database is already in the memory. I was assuming that the database had been created in the app itself, so it will be in memory only
传递空值会创建一个新的内存数据库。这意味着一个新的、空的数据库没有持久化到磁盘。当数据库关闭时,数据就没有了。如果您创建另一个内存数据库,它将看不到其他内存数据库中的数据。
But it works here but not in the other case.
可能是因为另一个助手在其 onCreate()
中填充了数据库,而另一个没有。
Further, what is the name of database created by default?
没有。内存数据库没有名称。命名数据库以您提供的名称存储在磁盘上。
我一直在使用 Android 和 SQLite 数据库开发应用程序。 但我有一些问题。 这是我的代码:
SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
SQLiteDatabase database = SQLiteHelperMoney1.getReadableDatabase();
Cursor cursorCategory = database.query(false, "CATEGORY", null, null, null, null, null, null, null);
SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, null, null, 1);
SQLiteDatabase database2 = SQLiteHelperMoney2.getReadableDatabase();
Cursor cursorTransaction = database2.query(false, "TRANSACTIONS", null, null, null, null, null, null, "10");
现在的问题是,在我的游标 cursorCategory
中,我得到了存储在我数据库的 Category
table 中的数据。但是我在cursorTransaction
中没有得到任何数据,为什么会这样??
此外,在实例化我的 SQLiteHelperMoney2
对象时,如果我将 SQLiteHelperMoney2
对象的实例化更改为此:
SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, "TRANSACTIONS", null, 1);
然后我从 Transaction
table 获取数据,在我的 cursorTransaction
.
但是为什么会这样呢??根据文档,在 SQLiteOpenHelper 的构造函数中,第二个参数是数据库的名称,而不是 table 的名称。 那么为什么在字段名称数据库中给出 TABLE 的名称会给出正确答案?
此外,文档说在 SQLiteOpenHelper 的构造函数中,如果数据库已经在内存中,您可以在字段 "NAME" 中指定 null
。我假设数据库是在应用程序本身中创建的,所以它只会在内存中,这就是为什么我在这里将第二个参数指定为 null :
SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
但它在这里有效,但在其他情况下无效。
另外,默认创建的数据库名称是什么?因为我没有在 SQLiteOpenHelper 的 onCreate
函数中指定它。我可以更改我的数据库的名称吗??
编辑:
onCreate
方法:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE CATEGORY (_ID INTEGER PRIMARY KEY, NAME TEXT)");
db.execSQL("CREATE TABLE TRANSACTIONS (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, CATEGORY TEXT, NOTE TEXT, TIME TEXT, EXPENSE INTEGER)");
db.execSQL("CREATE TABLE BUDGET (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, TIME TEXT)");
ContentValues cv = new ContentValues();
cv.put("NAME", "FOOD");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "TRAVEL");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "INCOME");/*INCOME*/
db.insert("CATEGORY", null, cv);
cv.put("NAME", "TRANSPORTATION");
db.insert("CATEGORY", null, cv);
cv.put("NAME", "MOVIE");
db.insert("CATEGORY", null, cv);
}
@laalto:我在这里阅读了您的回答:When is SQLiteOpenHelper onCreate() / onUpgrade() run?
而且我对磁盘上和内存中的数据库有点困惑。在我的 onCreate
方法中,我在 Category
table 中添加了行,因为我相信 onCreate 方法在应用程序的生命周期中只被调用一次,即当它是 运行 第一次,用于创建数据库。
这就是为什么在我的 onCreate
方法中我将数据添加到我的 CATEGORY
table,因为我希望它有一些初始数据。
而且我相信当我们对 getReadableDatabase()
和 getWritableDatabase()
进行替代调用时,它 returns 为我们提供了先前创建的数据库的副本,或者可能是一个句柄原始数据库并在完成时保存这些更改。但是,在阅读您的回答后,似乎 onCreate
方法每次都是 运行。如果每次都是 运行 数据存储在哪里,因为我们关闭数据库时会丢失数据库??
Now the problem is, that in my cursor cursorCategory I get the data that is stored in the Category table of my database. But I do not get any data in the cursorTransaction, why does this happen??
查询returns您刚创建的内存数据库中没有数据。
Furthermore, while instantiating my SQLiteHelperMoney2 object, if I change the instantiation of my SQLiteHelperMoney2 object to this :
...
But why does this happen??
查询 returns 先前创建的磁盘数据库中的一些数据。
出于某种巧合,数据库文件的名称与 table 相同。卸载并重新安装您的应用程序,或在应用程序管理器中清除其数据以删除可能仍然存在的旧数据库文件。
Furthermore, the documentation says that in the constructor for SQLiteOpenHelper, in the field "NAME" you can specify null if the database is already in the memory. I was assuming that the database had been created in the app itself, so it will be in memory only
传递空值会创建一个新的内存数据库。这意味着一个新的、空的数据库没有持久化到磁盘。当数据库关闭时,数据就没有了。如果您创建另一个内存数据库,它将看不到其他内存数据库中的数据。
But it works here but not in the other case.
可能是因为另一个助手在其 onCreate()
中填充了数据库,而另一个没有。
Further, what is the name of database created by default?
没有。内存数据库没有名称。命名数据库以您提供的名称存储在磁盘上。