/*簡單約束*/
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL);
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, age INTEGER);
CREATE TABLE IF NOT EXISTS t_student(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER DEFAULT 1);
/*分頁*/
SELECT * FROM t_student ORDER BY id ASC LIMIT 30, 10;
/*排序*/
SELECT * FROM t_student WHERE score > 50 ORDER BY age DESC;
SELECT * FROM t_student WHERE score < 50 ORDER BY age ASC , score DESC;
/*計(jì)量*/
SELECT COUNT(*) FROM t_student WHERE age > 50;
/*別名*/
SELECT name as myName, age as myAge, score as myScore FROM t_student;
SELECT name myName, age myAge, score myScore FROM t_student;
SELECT s.name myName, s.age myAge, s.score myScore FROM t_student s WHERE s.age > 50;
/*查詢*/
SELECT name, age, score FROM t_student;
SELECT * FROM t_student;
/*修改指定數(shù)據(jù)*/
UPDATE t_student SET name = 'MM' WHERE age = 10;
UPDATE t_student SET name = 'WW' WHERE age is 7;
UPDATE t_student SET name = 'XXOO' WHERE age < 20;
UPDATE t_student SET name = 'NNMM' WHERE age < 50 and score > 10;
/*刪除數(shù)據(jù)*/
DELETE FROM t_student;
/*更新數(shù)據(jù)*/
UPDATE t_student SET name = 'LNJ';
/*插入數(shù)據(jù)*/
INSERT INTO t_student(age, score, name) VALUES ('28', 100, 'jonathan');
INSERT INTO t_student(name, age) VALUES ('lee', '28');
INSERT INTO t_student(score) VALUES (100);
/*插入數(shù)據(jù)*/
INSERT INTO t_student(name, age, score) VALUES ('lee', '28', 100);
/*添加主鍵*/
CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, score REAL);
/*添加主鍵*/
CREATE TABLE IF NOT EXISTS t_student (id INTEGER, name TEXT, age INTEGER, score REAL, PRIMARY KEY(id));
/*刪除表*/
DROP TABLE IF EXISTS t_student;
/*創(chuàng)建表*/
CREATE TABLE IF NOT EXISTS t_student(id INTEGER , name TEXT, age , score REAL);
1.打開數(shù)據(jù)庫
int sqlite3_open(
const char *filename, // 數(shù)據(jù)庫的文件路徑
sqlite3 **ppDb // 數(shù)據(jù)庫實(shí)例
);
2.執(zhí)行任何SQL語句
int sqlite3_exec(
sqlite3*, // 一個(gè)打開的數(shù)據(jù)庫實(shí)例
const char *sql, // 需要執(zhí)行的SQL語句
int (*callback)(void*,int,char**,char**), // SQL語句執(zhí)行完畢后的回調(diào)
void *, // 回調(diào)函數(shù)的第1個(gè)參數(shù)
char **errmsg // 錯(cuò)誤信息
);
3.檢查SQL語句的合法性(查詢前的準(zhǔn)備)
int sqlite3_prepare_v2(
sqlite3 *db, // 數(shù)據(jù)庫實(shí)例
const char *zSql, // 需要檢查的SQL語句
int nByte, // SQL語句的最大字節(jié)長度
sqlite3_stmt **ppStmt, // sqlite3_stmt實(shí)例鞋囊,用來獲得數(shù)據(jù)庫數(shù)據(jù)
const char **pzTail
);
4.查詢一行數(shù)據(jù)
int sqlite3_step(sqlite3_stmt*); // 如果查詢到一行數(shù)據(jù)止后,就會返回SQLITE_ROW
5.利用stmt獲得某一字段的值(字段的下標(biāo)從0開始)
double sqlite3_column_double(sqlite3_stmt*, int iCol); // 浮點(diǎn)數(shù)據(jù)
int sqlite3_column_int(sqlite3_stmt*, int iCol); // 整型數(shù)據(jù)
sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); // 長整型數(shù)據(jù)
const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); // 二進(jìn)制文本數(shù)據(jù)
const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); // 字符串?dāng)?shù)據(jù)