博客
关于我
sqlite3笔记
阅读量:486 次
发布时间:2019-03-07

本文共 2405 字,大约阅读时间需要 8 分钟。

SQLite数据库安装与使用指南

1. 安装 SQLite

1.1 本地安装

如果你使用的是 Ubuntu 或 Debian 系统,可以通过以下命令安装 SQLite:

sudo dpkg -i *.deb

安装完成后,运行以下命令查看 SQLite 版本:

sqlite3 --version

这将显示 SQLite 的版本信息,确认安装成功。

1.2 在线安装

如果你的系统已经支持包管理工具(如 apt-get),并且你能连接互联网,可以通过以下命令在线安装 SQLite:

sudo apt-get install sqlite3

2. 创建数据库

2.1 创建数据库

运行以下命令创建一个名为 student.db 的数据库:

sqlite3 student.db

注意:数据库文件名可以任意命名,但 student.db 是常用的选择。

3. 数据库基本操作

3.1 系统命令

  • 查看帮助文档:
    .help
  • 退出 SQLite 会话:
    .quit
  • 退出提示:
    .exit
  • 查看当前打开的数据库:
    .databases

3.2 SQL 命令

3.2.1 创建表

  • 创建一个新表 stu
    create table stu(    id integer,    name char,    score integer);
  • 如果表已存在,使用以下命令创建(不会报错):
    create table if not exists student(    id integer,    name char,    score integer);

3.2.2 插入数据

  • 插入一条记录:
    insert into stu values(100, 'zhangsan', 80);
  • 插入特定字段的值:
    insert into stu (name, score) values('zhangsan', 100);

3.2.3 查询数据

  • 查询所有记录:
    select * from stu;
  • 查询指定字段:
    select name from stu;
  • 根据条件查询:
    select * from stu where score = 80;

3.2.4 删除记录

  • 删除整个表:
    delete from stu;
  • 删除特定条件的记录:
    delete from stu where name = 'zhangsan';

3.2.5 修改字段

  • 更新特定记录:
    update stu set name = 'cainiao' where id = 1002;

3.2.6 表操作

  • 添加一列:
    alter table stu add column address char;
  • 删除一列(注意:SQLite 不支持直接删除列,需通过以下步骤操作):
  • 创建新表:
    create table stdu1 as select id, name, score from stu;
  • 删除旧表:
    drop table stu;
  • 重命名新表为旧表名:
    alter table stu1 rename to stu;

4. 编译 SQLite 应用程序

4.1 安装依赖

安装 SQLite 开发库:

sudo apt-get install libsqlite3-dev

4.2 编译源代码

将代码保存为 student_manager.c,运行以下命令编译:

gcc student_manager.c -o student_manager -lsqlite3

5. 使用示例

5.1 创建表

#include 
#include
sqlite3 *db;char *database_path = "student.db";char *table_name = "student";int create_table(char *table_name) { char *sql = "create table if not exists %s(id integer, name char, score integer);", table_name; if (sqlite3_exec(db, sql, NULL, 0, NULL) != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); return -1; } printf("Table created successfully\n"); return 0;}int main() { // 初始化数据库 sqlite3_open(database_path, &db); if (sqlite3_exec(db, "PRAGMA journal_mode = off;", NULL, 0, NULL) != SQLITE_OK) { fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db)); return -1; } // 创建表 create_table(table_name); // ... 其他操作 ... sqlite3_close(db); return 0;}

6. 常见问题

  • 权限问题:确保你有权限访问数据库文件。
  • 未安装依赖:检查是否安装了 libsqlite3-dev
  • 数据库文件丢失:请确保数据库文件存在且不被删除。

通过以上步骤,你应该能够顺利安装 SQLite 并使用它来管理你的数据。如果有任何问题,请参考 SQLite官方文档或社区获取帮助。

转载地址:http://lozcz.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>