几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
1.创建表
CREATE TABLE IF NOT EXISTS UserTable (username TEXT primary key,password TEXT,email TEXT);
2.插入一条数据
INSERT OR REPLACE INTO UserTable (username , password,email) VALUES (?,?,?);
3.更新一条数据
UPDATE UserTable set password = '123456' where username = 'wxhl';
4.查询数据
SELECT username,password,email FROM UserTable where username = 'wxhl'
5.删除数据
DELETE FROM UserTable WHERE username='wxhl'
6.模糊查询
SELECT *FROM UserTable Where username is LIKE "流%"
7.倒叙排列(默认正)
SELECT *FROM UserTable ORDER BY sage DESC
//使用数据库的流程
/*
* 1、打开数据库
* 2、准备语句
* 3、读取(绑定)列
* 4、语句完结
* 5、关闭数据库
*/
//sqlite3.0 API
1.导入 libsqlite3.0.dylib
2. sqlite3_open() //打开数据库
3. sqlite3_close() //关闭数据库
4. sqlite3_exec() //执行sql语句,例如创建表
5. sqlite3_prepare_v2() //编译SQL语句
6. sqlite3_step() //执行SQL语句
7. sqlite3_finalize() //结束sql语句
8. sqlite3_bind_text() //往数据库占位符上填充数据
9. sqlite3_column_text() //查询字段上的数据
9. SQLITE_OK 执行结果成功、 SQLITE_ERROR 执行失败
实例:
1. sqlite3_open([sandboxPath UTF8String], &dbPointer);
2. sqlite3_close(dbPointer);
3. sqlite3_exec (database, [sql UTF8String],NULL, NULL, &errorMsg)
4. sqlite3_prepare_v2(contactDB, "SELECT *FROM contact1", -1, &stmt, NULL);
5. while (sqlite3_step(statement) == SQLITE_ROW) {
6. sqlite3_finalize(statement);
7. sqlite3_bind_text(stmt, 1, [address.name UTF8String], -1, NULL);