我正在尝试为测验建立数据库。我想将[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]);
}
}
最好的问候, 鲁斯本
答案 0 :(得分:0)
更改您的
return await db
.rawQuery('SELECT * FROM $table WHERE $quizID = $quizName');
为此
return await db
.rawQuery('SELECT * FROM $table WHERE $quizID = ?', [quizName]);
在$ quizName之前和之后,您基本上都缺少引号,但是最好使用?参数。