sql - 查询列包含特定单词的所有列不起作用?
问题描述
我正在尝试为测验设置数据库。我想将 [quizName] 存储在一列中,并将 [quizQuestions] 存储在第二列中。
问题是即使创建了数据库并且列存在,我也会收到错误“没有这样的列:COLUMN-NAME ”
同时,如果我查询表中的所有数据,我实际上是从每一列中获取所有值。我真的找不到错误,我做错了什么?
class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 7;
static final table = 'my_table';
static final quizID = 'quizID';
static final quizQuestions = 'quizQuestions';
// make this a singleton class
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// only have a single app-wide reference to the database
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database;
}
// this opens the database (and creates it if it doesn't exist)
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion, onCreate: _onCreate);
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute('CREATE TABLE $table ($quizID TEXT, $quizQuestions TEXT)');
}
// Helper methods
// Inserts a row in the database where each key in the Map is a column name
// and the value is the column value. The return value is the id of the
// inserted row.
Future<int> insert(Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row);
}
// All of the rows are returned as a list of maps, where each map is
// a key-value list of columns.
Future<List<Map<String, dynamic>>> queryAllRows() async {
Database db = await instance.database;
return await db.query(table);
}
// All of the methods (insert, query, update, delete) can also be done using
// raw SQL commands. This method uses a raw query to give the row count.
Future<int> queryRowCount() async {
Database db = await instance.database;
return Sqflite.firstIntValue(
await db.rawQuery('SELECT COUNT(*) FROM $table'));
}
// Below method is not finding column
Future<List<Map<String, dynamic>>> queryQuestions(String quizName) async {
Database db = await instance.database;
return await db
.rawQuery('SELECT * FROM $table WHERE $quizID = $quizName');
}
// We are assuming here that the id column in the map is set. The other
// column values will be used to update the row.
Future<int> update(Map<String, dynamic> row) async {
Database db = await instance.database;
int id = row[quizID];
return await db.update(table, row, where: '$quizID = ?', whereArgs: [id]);
}
// Deletes the row specified by the id. The number of affected rows is
// returned. This should be 1 as long as the row exists.
Future<int> delete(int id) async {
Database db = await instance.database;
return await db.delete(table, where: '$quizID = ?', whereArgs: [id]);
}
}
最好的问候, 鲁斯本
解决方案
改变你的
return await db
.rawQuery('SELECT * FROM $table WHERE $quizID = $quizName');
为此
return await db
.rawQuery('SELECT * FROM $table WHERE $quizID = ?', [quizName]);
您在 $quizName 之前和之后基本上都缺少引号,但最好使用 ? 反正参数。
推荐阅读
- python - 你如何改变python中可以添加到整数的内容?
- python - 这是尝试代理的正确方法吗?
- node.js - 节点js mongodb查询
- python-3.x - 我在 python 中创建了 2 种不同的 2D 列表方式,然后用相同的方式更新,但我得到不同的结果
- javascript - NGX 在 Angular 9 中翻译
- next.js - 错误:失败的道具类型:道具`href`在` `中需要`string`或`object`,但得到`undefined`
- unity3d - 如何使用 google cardboard 在 VR 中开始?
- apache-camel - Camel fromF文件路由
- flutter - 更新 Android Studio 后 Flutter 插件不起作用
- javascript - 鼠标滚轮后刷卡器不会恢复自动播放