sqlite3 简单使用

H:/Ruby192/bin>sqlite3 a.db
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .database;
Error: unknown command or invalid arguments:  "database;". Enter ".help" for he
p
sqlite> .database
seq  name             file

---  ---------------  ---------------------------------------------------------

0    main             H:/Ruby192/bin/a.db

sqlite> .table
a
sqlite> insert into a(1)
   ...> ;
Error: near "1": syntax error
sqlite> select * from a;
sqlite> selet * from a
   ...> ;
Error: near "selet": syntax error
sqlite> select * from a;
sqlite> insert a (id) values(1);
Error: near "a": syntax error
sqlite> insert a (id) values(1);
Error: near "a": syntax error
sqlite> insert into a (id) values(1);
sqlite> select * from a;
1
sqlite> insert into a (id) values(12);
sqlite> insert into a (id) values(13);
sqlite> insert into a (id) values(15);
sqlite> select * from a;
1
12
13
15
sqlite> alter table a (add column b varchar(10));
Error: near "(": syntax error
sqlite> alter table a add column b varchar(10);
sqlite> insert into a (id) values(16,'a');
Error: 2 values for 1 columns
sqlite> insert into a (id,b) values(16,'a');
sqlite> select * from a;
1|
12|
13|
15|
16|a
sqlite> select top 1 * from a ;
Error: near "1": syntax error
sqlite> select  * from a limit 1;
1|
sqlite> select  * from a order by id desc limit 1;
16|a
sqlite> select  * from a order by id desc limit 1 to 2;
Error: near "to": syntax error
sqlite> select  * from a order by id desc limit 10;
16|a
15|
13|
12|
1|
sqlite>

 

 

 

OS X自从10.4后把SQLite这套相当出名的数据库软件,放进了作业系统工具集里。OS X包装的是第三版的SQLite,又称SQLite3。这套软件有几个特色:

  • 软件属于公共财(public domain),SQLite可说是某种「美德软件」(virtueware),作者本人放弃着作权,而给使用SQLite的人以下的「祝福」(blessing):
    • May you do good and not evil. 愿你行善莫行恶
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中使用SQLite3数据库可以通过SQLite3的C接口来实现。下面是一个简单的示例代码,演示了如何在C++中使用SQLite3进行数据库操作: ```cpp #include <iostream> #include <sqlite3.h> int main() { sqlite3* db; int rc = sqlite3_open("test.db", &db); if (rc) { std::cerr << "Cannot open database: " << sqlite3_errmsg(db) << std::endl; return rc; } char* errMsg; rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);", nullptr, nullptr, &errMsg); if (rc != SQLITE_OK) { std::cerr << "SQL error: " << errMsg << std::endl; sqlite3_free(errMsg); return rc; } rc = sqlite3_exec(db, "INSERT INTO users (name, age) VALUES ('John', 25);", nullptr, nullptr, &errMsg); if (rc != SQLITE_OK) { std::cerr << "SQL error: " << errMsg << std::endl; sqlite3_free(errMsg); return rc; } sqlite3_stmt* stmt; rc = sqlite3_prepare_v2(db, "SELECT * FROM users;", -1, &stmt, nullptr); if (rc != SQLITE_OK) { std::cerr << "SQL error: " << sqlite3_errmsg(db) << std::endl; return rc; } while (sqlite3_step(stmt) == SQLITE_ROW) { int id = sqlite3_column_int(stmt, 0); const unsigned char* name = sqlite3_column_text(stmt, 1); int age = sqlite3_column_int(stmt, 2); std::cout << "ID: " << id << ", Name: " << name << ", Age: " << age << std::endl; } sqlite3_finalize(stmt); sqlite3_close(db); return 0; } ``` 上述代码首先打开名为"test.db"的数据库文件,如果文件不存在则会创建一个新的数据库文件。然后创建一个名为"users"的表,表中包含id、name和age三个字段。接着插入一条记录,然后查询并输出所有记录的内容。 需要注意的是,上述代码只是一个简单的示例,实际使用时可能需要更多的错误处理和安全性检查。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值