无法执行 sqlite SQL 文件来创建数据库
Cannot execute an sqlite SQL file to create a database
我有一个sql(文本文件),其中包含一个长事务来创建数据库。
我正在使用Kompex sqlite c ++库:
// open database
Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase(CT2A(strDBFilename), SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
// create statement instance for sql queries/statements
Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase);
strSQLCreateDB=get_file_contents(strSQLFilename).c_str(); // load the SQL file
try {
pStmt->Sql(strSQLCreateDB);
pStmt->ExecuteAndFree();
} catch(Kompex::SQLiteException &exception)
{
std::cerr << "Exception Occured" << std::endl;
exception.Show();
}
pDatabase->Close();
创建数据库时为 0 字节且没有错误。即使没有 kompex 库,我也对任何示例代码感兴趣,以执行创建数据库的此 sql 事务。
从SQLite浏览器执行数据库会创建数据库而不会出错。
这是 SQL 命令文件的一部分
/* Disable Foreign Keys */
pragma foreign_keys = off;
/* Begin Transaction */
begin transaction;
/* Database [scanlog(1)] */
pragma auto_vacuum=0;
pragma encoding='UTF-8';
pragma page_size=1024;
/* Drop table [ApplicationNames] */
drop table if exists [ApplicationNames];
/* Table structure [ApplicationNames] */
CREATE TABLE [ApplicationNames] (
[Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[ApplicationName] TEXT,
[Icon] TEXT);
CREATE INDEX [IndexApps] ON [ApplicationNames] ([Id], [ApplicationName]);
/* Data [ApplicationNames] */
insert into [ApplicationNames] values(1, 'Windows System', null);
insert into [ApplicationNames] values(2, 'Internet Explorer', null);
insert into [ApplicationNames] values(3, 'Google Chrome', null);
/* Commit Transaction */
commit transaction;
/* Enable Foreign Keys */
pragma foreign_keys = on;
这
似乎有效:
// open connection to a DB
if (SQLITE_OK != (ret = sqlite3_open_v2(CT2A(strDBFilename), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)))
{
TRACE("Failed to open SQLite conn: %dn", ret);
break;
}
// prepare the statement
if (SQLITE_OK != (ret = sqlite3_prepare_v2(pDb, CT2A(strSQLCreateDB), -1, &query, NULL)))
{
TRACE("Failed to prepare SQLite script: %d, %sn", ret, sqlite3_errmsg(pDb));
break;
}
if (SQLITE_OK != (ret=sqlite3_exec(pDb, CT2A(strSQLCreateDB), NULL, 0, NULL)))
{
TRACE("Failed to execute SQLite script: %d, %sn", ret, sqlite3_errmsg(pDb));
break;
}
您的问题是 Sql
和 sqlite3_prepare
函数仅执行字符串中的第一个 SQL 命令。
要在字符串中执行多个命令,必须使用 sqlite3_exec
:
if (SQLITE_OK != (ret = sqlite3_open_v2(CT2A(strDBFilename), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)))
{
TRACE("Failed to open SQLite conn: %dn", ret);
break;
}
if (SQLITE_OK != (ret = sqlite3_exec(pDb, CT2A(strSQLCreateDB), NULL, 0, NULL)))
{
TRACE("Failed to execute SQLite script: %d, %sn", ret, sqlite3_errmsg(pDb));
break;
}
view.sql
有语句
从文件中获取数据库定义,这种方式很容易管理表、视图和触发器的定义:
{
std::ifstream ifs("view.sql");
std::string content(
(std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>())
);
size_t n_pos,o_pos=0;
n_pos = content.find(";");
while (n_pos!=content.npos) {
++n_pos;
sqlite::execute(con,content.substr(o_pos,n_pos-o_pos).c_str(),true);
o_pos=n_pos;
n_pos= content.find(";",o_pos);
}
}
sqlite::execute(con,"VACUUM;",true);
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- SqLite c++,可以创建数据库,但不能将 anthing 插入到表中
- 在iOS应用程序中创建数据库
- 查询数据库以在 C++ 中创建自定义类集合的最佳方法
- PostgreSQL:如何为我的单元测试创建临时用户和数据库
- 需要为SQLite3数据库创建索引(索引)
- HTTP post 使用 libcurl 创建 InfluxDB 数据库
- 使用用户输入创建JSON数据库
- 在数据库文件中创建10条空白记录
- 如何在 c++ 程序中创建内部数据库
- 通过 ODBC 创建数据库
- 在C++中创建 Sqlite 数据库
- 使用IBPP使用C 创建Firebird数据库
- 使用QT C 为Sevaral表单创建常见的数据库连接
- 从命令行创建Intellisense数据库
- 无法执行 sqlite SQL 文件来创建数据库
- 如何使用C++在 Clion 中创建数据库
- 在 BlackBerry 10 上创建数据库
- Qt:在用户空间中存储/创建数据库