我如何在Microsoft Visual c++ 2010 Express项目中创建本地数据库
How can I create a local database inside a Microsoft Visual C++ 2010 Express project?
如何在Microsoft Visual c++ 2010 Express项目中创建本地数据库?
我在网上找不到这个简单的答案。我找到的唯一答案是Visual Studio:使用project>添加新项目>本地数据库。但是这个选项在Visual c++ 2010 Express版中不可用。我尝试安装"Microsoft SQL Server Compact 4"answers"Microsoft SQL Server denalii",以及更新"Microsoft Visual c++ 2010 express";from "Windows Update"
好了,我终于有办法了。很遗憾,我必须回答我自己的问题……
我使用SQLite库(http://www.sqlite.org/)。这有点复杂,因为sqlite文档有点模糊,但我做了如下:
- 下载sqlitedll*.zip -解压。def和。dll文件。
- 用像"c:program . exe"这样的命令生成lib文件文件微指令~ 1 vc98 bin lib/def: sqlite3.def"。用命令来做吗提示,在带有.def文件的目录中,使用适当的lib.exe的路径。您可能需要先运行vcvars32.bat,即也在bin目录中。将生成的.lib复制到适当的并将其设置为vc++中的库目录。(或者在一个基于)。
- 下载sqlite-source*.zip文件,解压缩sqlite3.h文件从内部到合适的目录。将其设置为包含目录在vc++。(同样,您可以在每个项目的基础上进行。)
- 在您的项目中,按需要添加#include,添加sqlite3.lib将sqlite3.dll复制到您的项目的可执行目录中或工作目录,你应该准备好了。
然后,很容易使用no-out查询,但如果您想使用SQL"SELECT",例如,您可以使用以下代码:
std::string queries;
// A prepered statement for fetching tables
sqlite3_stmt *stmt;
// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;
// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
int retval = sqlite3_open("local.db",&handle);
// If connection failed, handle returns NULL
if(retval){
System::Windows::Forms::MessageBox::Show("Database connection failed");
return;
}
// Create the SQL query for creating a table
char create_table[100] = "CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)";
// Execute the query for creating the table
retval = sqlite3_exec(handle,create_table,0,0,0);
// Insert first row and second row
queries = "INSERT INTO users VALUES('manish','manish',1)";
retval = sqlite3_exec(handle,queries.c_str(),0,0,0);
queries = "INSERT INTO users VALUES('mehul','pulsar',0)";
retval = sqlite3_exec(handle,queries.c_str(),0,0,0);
// select those rows from the table
queries = "SELECT * from users";
retval = sqlite3_prepare_v2(handle,queries.c_str(),-1,&stmt,0);
if(retval){
System::Windows::Forms::MessageBox::Show("Selecting data from DB Failed");
return ;
}
// Read the number of rows fetched
int cols = sqlite3_column_count(stmt);
while(1){
// fetch a row’s status
retval = sqlite3_step(stmt);
if(retval == SQLITE_ROW){
// SQLITE_ROW means fetched a row
// sqlite3_column_text returns a const void* , typecast it to const char*
for(int col=0 ; col<cols;col++){
const char *val = (const char*)sqlite3_column_text(stmt,col);
System::Windows::Forms::MessageBox::Show(stdstr2systemstr(sqlite3_column_name(stmt,col))+" = "+stdstr2systemstr(val));
}
}
else
if(retval == SQLITE_DONE){
// All rows finished
System::Windows::Forms::MessageBox::Show("All rows fetched");
break;
}
else{
// Some error encountered
System::Windows::Forms::MessageBox::Show("Some error encountered");
return ;
}
}
// Close the handle to free memory
sqlite3_close(handle);
我希望这个信息是有用的!
来源:
- http://www.gamedev.net/topic/332251-sqlite3-and-visual-c/page_ p _3157685 # entry3157685
- http://milky.manishsinha.net/2009/03/30/sqlite-with-c/
相关文章:
- 在其他文件中创建类时在 c++ 项目中不起作用
- 如何从支持linux/mac操作系统的第三方代码为我的c#项目创建dll
- 无法为编译 SFML 项目创建 cmake 规则
- QT:我应该使用哪个对象/项目创建可点击图标
- 为Qt项目创建一个标志或使用QLoggingCategory
- 从现有 mfc 项目创建 dll
- 为Qt中现有的C++项目创建UI的某些部分
- C为多个项目创建一个生成目录
- 如何在c++XE5 embacadero中为项目创建静态构建
- 从我的项目创建EXE文件
- 从 Visual Studio 2012 项目创建".o"文件
- Eclipse C++ 项目创建向导中的"Finish"命令不起作用
- 如何为现有项目创建Makefile
- 如何仅为解决方案中的一个项目创建配置
- 我应该在什么时候为我的项目创建一个存储库
- 为依赖于LLVM的项目创建cmakelist
- 为项目创建Include-Dependencies
- 使用 /Command 选项从命令行为 Visual Studio C++ 项目创建一个新文件
- 算法中的莫名其妙的行为,应根据列表的项目创建 4 个列表
- 如何在Visual Studio c++ ATL项目创建的COM .dll中添加新函数