数据库增删改查代码
2016-03-23 22:34
447 查看
-(NSArray *)getAllStundets
{
//1创建临时集合 存储学生信息
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:20];
//2打开数据库
sqlite3 *sql = [DBManager openDB];
//3执行命令语句
sqlite3_stmt *stmt;
//4预处理操作 查询表里的所有记录
if (sql!=nil) {
int num = sqlite3_prepare_v2(sql, "select * from student", -1, &stmt, nil);
//5是否可以操作表
if (num == SQLITE_OK) {
//6循环便利表中的记录 即检索每一行
while ( SQLITE_ROW == sqlite3_step(stmt)) {
//7根据指定索引找到指定行的信息
int stuid = sqlite3_column_int(stmt, 0);
NSString *stuname = [NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 1)];
NSString *stupwd = [NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 2)];
//8创建临时学生类型对象
Student *stu = [[Student alloc] init];
//9将数据表中的信息 存储到实体类的属性中
stu.stuid = stuid;
stu.stuname = stuname;
stu.stupwd = stupwd;
//将试题类型添加到数组中
[arr addObject:stu];
}
}
}
//10返回集合信息,即一组 学生类型的集合
return arr;
}
-(BOOL)addStudents:(Student *)stu
{
BOOL bol = NO;
sqlite3 *sql = [DBManager openDB];
sqlite3_stmt *stmt;
int num = sqlite3_prepare_v2(sql, "insert into student(stuname,stupwd) values(?,?)", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [stu.stuname UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [stu.stupwd UTF8String], -1, nil);
if (num == SQLITE_OK) {
if (SQLITE_DONE == sqlite3_step(stmt)) {
bol = YES;
}
}
return bol;
}
-(BOOL)updateStudents:(Student *)stu
{
BOOL bol = NO;
sqlite3 *sql = [DBManager openDB];
sqlite3_stmt *stmt;
int ret = sqlite3_prepare_v2(sql, "update student set stuname = ?, stupwd = ? where stuid = ? ", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [stu.stuname UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [stu.stupwd UTF8String], -1, nil);
sqlite3_bind_int(stmt, 3, stu.stuid);
if (ret == SQLITE_OK) {
if (SQLITE_DONE == sqlite3_step(stmt)) {
bol = YES;
}
}
return bol;
}
-(BOOL)removeStundets:(Student *)stu
{
BOOL bol = NO;
sqlite3 *sql = [DBManager openDB];
sqlite3_stmt *stmt;
int ret = sqlite3_prepare_v2(sql, "delete from student where stuname = ? or stupwd = ? or stuid = ?", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [stu.stuname UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [stu.stupwd UTF8String], -1, nil);
sqlite3_bind_int(stmt, 3, stu.stuid);
if (ret == SQLITE_OK) {
if (SQLITE_DONE == sqlite3_step(stmt)) {
bol = YES;
}
}
return bol;
}
{
//1创建临时集合 存储学生信息
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:20];
//2打开数据库
sqlite3 *sql = [DBManager openDB];
//3执行命令语句
sqlite3_stmt *stmt;
//4预处理操作 查询表里的所有记录
if (sql!=nil) {
int num = sqlite3_prepare_v2(sql, "select * from student", -1, &stmt, nil);
//5是否可以操作表
if (num == SQLITE_OK) {
//6循环便利表中的记录 即检索每一行
while ( SQLITE_ROW == sqlite3_step(stmt)) {
//7根据指定索引找到指定行的信息
int stuid = sqlite3_column_int(stmt, 0);
NSString *stuname = [NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 1)];
NSString *stupwd = [NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 2)];
//8创建临时学生类型对象
Student *stu = [[Student alloc] init];
//9将数据表中的信息 存储到实体类的属性中
stu.stuid = stuid;
stu.stuname = stuname;
stu.stupwd = stupwd;
//将试题类型添加到数组中
[arr addObject:stu];
}
}
}
//10返回集合信息,即一组 学生类型的集合
return arr;
}
-(BOOL)addStudents:(Student *)stu
{
BOOL bol = NO;
sqlite3 *sql = [DBManager openDB];
sqlite3_stmt *stmt;
int num = sqlite3_prepare_v2(sql, "insert into student(stuname,stupwd) values(?,?)", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [stu.stuname UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [stu.stupwd UTF8String], -1, nil);
if (num == SQLITE_OK) {
if (SQLITE_DONE == sqlite3_step(stmt)) {
bol = YES;
}
}
return bol;
}
-(BOOL)updateStudents:(Student *)stu
{
BOOL bol = NO;
sqlite3 *sql = [DBManager openDB];
sqlite3_stmt *stmt;
int ret = sqlite3_prepare_v2(sql, "update student set stuname = ?, stupwd = ? where stuid = ? ", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [stu.stuname UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [stu.stupwd UTF8String], -1, nil);
sqlite3_bind_int(stmt, 3, stu.stuid);
if (ret == SQLITE_OK) {
if (SQLITE_DONE == sqlite3_step(stmt)) {
bol = YES;
}
}
return bol;
}
-(BOOL)removeStundets:(Student *)stu
{
BOOL bol = NO;
sqlite3 *sql = [DBManager openDB];
sqlite3_stmt *stmt;
int ret = sqlite3_prepare_v2(sql, "delete from student where stuname = ? or stupwd = ? or stuid = ?", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [stu.stuname UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [stu.stupwd UTF8String], -1, nil);
sqlite3_bind_int(stmt, 3, stu.stuid);
if (ret == SQLITE_OK) {
if (SQLITE_DONE == sqlite3_step(stmt)) {
bol = YES;
}
}
return bol;
}
相关文章推荐
- 数据库增 删 改 查
- MySQL的varchar定义长度到底是字节还是字符
- 【MySQL】安全管理 数据库维护 改善性能
- 【MySQL】插入数据 创建和操纵表
- 如何创建mysql索引以及索引的优缺点
- 【MySQL】事务处理 全球化和本地化(字符集和校对)
- 【MySQL】存储过程 游标 触发器
- 联合memcache,repcached,magent,monit四个组件搭建高可用集群方案
- 【MySQL】SELECT语句 联结表 高级联结 组合查询 全文本搜索
- 【MySQL】SELECT语句 计算字段 数据处理函数 汇总函数 分组数据 子查询
- oracle字符集的查看和修改(以修改redhat库字符集为例)
- 【MySQL】 SELECT语句 检索 排序 过滤 通配符搜索 正则表达式搜索
- php 封装Memcache 队列缓存类
- 数据库基本语句大全
- Mysql数据类型和常用字段总结
- 常用的sql语句
- MySql 操作总结
- 初识PL/SQL
- SQLServer中char、varchar、nchar、nvarchar的区别:
- mysql查询某字段出现次数大于n的所有数据信息,还有对列出前三位