Android SQLite 问题
我只是想将数据添加到数据库并检索它。
这就是我创建数据库表的方式:
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "isbn text not null, title text not null, "
+ "publisher text not null);";
db.execSQL(DATABASE_CREATE);
我使用以下函数添加数据:
public long insertTitle(String isbn, String title, String publisher)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialValues);
}
要检索所有数据,我使用以下函数:
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
null,
null,
null,
null,
null);
}
这非常有效。但是,如果我尝试检索给定“标题”列值的行,则会收到错误。我为此使用的代码是:
public Cursor getTitle(String rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_PUBLISHER
},
KEY_TITLE + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
我在 logcat 中遇到的错误是:
SQLiteException:no such column verizon: while compiling: select DISTINCTpublisher fromtitles where title=verizon;
I am just trying to add data to the database and retrieve it.
This is how i create the database table:
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "isbn text not null, title text not null, "
+ "publisher text not null);";
db.execSQL(DATABASE_CREATE);
I am adding data using the following function:
public long insertTitle(String isbn, String title, String publisher)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialValues);
}
To retrieve all data, i use the following function:
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
null,
null,
null,
null,
null);
}
And this works perfectly. However, if i try to retrieve a row given the value for "title" column, i get the error. The code that i use for this is :
public Cursor getTitle(String rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_PUBLISHER
},
KEY_TITLE + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
The error that i am getting in the logcat is:
SQLiteException:no such column verizon: while compiling: select DISTINCT publisher from titles where title=verizon;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改此:
<代码>
KEY_TITLE + “=” + rowId,
对此:
KEY_TITLE + "='" + rowId + "'",
但是,除非您转义 rowId,否则您可能会受到 sql 注入的影响(取决于您从何处获取 rowId。)
Change this :
KEY_TITLE + "=" + rowId,
to this:
KEY_TITLE + "='" + rowId + "'",
However, unless you're escaping the rowId, you could be subject to sql injection (depending on where you're getting the rowId from.)
光是看看错误消息就让我想知道是否应该有 verizon 周围的引号。
Just looking at the error message makes me wonder if there were supposed to be quotes around verizon.