Sqlite3 数据库使用
来源:互联网 发布:python快速开发平台 编辑:程序博客网 时间:2024/05/22 15:44
iphone本身是支持 Sqlite3 数据库的,在项目中导入libsqlite3.dylib。并创建数据库,在终端,创建数据库的方式:
---》mkdir SQL //创建SQL文件夹
---》cd SQL //进入SQL目录下
---》sqlite3 Student.sql//创建名为 Student.sql的数据库
//创建表
- (void)createTable{
sqlite3 * database;
NSArray *paths=NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
TRUE);
NSString * path=[paths objectAtIndex:0];
filePath=[path stringByAppendingPathComponent:kFileName];
NSLog(@"this is kFileName : %@",kFileName);
NSLog(@"init filePath : %@",filePath);
NSFileManager * fileManager=[NSFileManager defaultManager];
BOOL fileFinded=[fileManager fileExistsAtPath:filePath];
NSLog(@"this is fileFinded : %d",fileFinded);
if (fileFinded) {
NSLog(@"database is existed");
if (sqlite3_open([filePath UTF8String], &database)==SQLITE_OK) {
char *errorMsg;
NSString * createSQL=
@"create table newteacher (id integer primary key ,username text,password text)";
if (sqlite3_exec(database,
[createSQL UTF8String],
NULL,
NULL,
&errorMsg) == SQLITE_OK) {
NSLog(@"create ok....");
}
}
sqlite3_close(database);
}
}
//插入数据
- (void)insertDataToTalbe{
sqlite3 *database;
if (sqlite3_open([filePath UTF8String], &database)==SQLITE_OK) {
sqlite3_stmt * sqlStatement;
NSLog(@"-----11111111");
const char * sql="insert into newteacher (username,password) values (?,?)";
if (sqlite3_prepare(database, sql, -1, &sqlStatement, NULL)==SQLITE_OK) {
NSLog(@"-------222222");
sqlite3_bind_text(sqlStatement,
1,
[student.username UTF8String],
-1,
SQLITE_TRANSIENT);
sqlite3_bind_text(sqlStatement,
2,
[student.password UTF8String],
-1,
SQLITE_TRANSIENT);
}
sqlite3_step(sqlStatement);
sqlite3_finalize(sqlStatement);
}
sqlite3_close(database);
}
//查询数据
- (BOOL)selectDatabase{
sqlite3 * database;
sqlite3_stmt * compliedStatement=nil;
NSLog(@"11111");
if (sqlite3_open([filePath UTF8String], &database)==SQLITE_OK) {
NSLog(@"2222222");
const char * sql="select * from newteacher";
if (sqlite3_prepare(database, sql , -1, &compliedStatement, nil)==SQLITE_OK) {
NSLog(@"333333");
while (sqlite3_step(compliedStatement)==SQLITE_ROW) {
NSLog(@"--------%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(compliedStatement, 1)]) ;
}
sqlite3_step(compliedStatement);
}
sqlite3_close(database);
}
return YES;
}
方法不全面,练习时写下的。
- 使用小型数据库SQLite3
- sqlite3数据库使用
- Sqlite3 数据库使用
- sqlite3数据库使用
- ubuntu使用sqlite3数据库
- SQLite3 数据库使用
- SQLite3 数据库使用
- sqlite3数据库的使用
- 如何使用SQLite3数据库
- sqlite3 数据库使用
- sqlite3数据库基本使用
- sqlite3数据库的使用
- sqlite3数据库的使用
- 使用sqlite3 创建数据库
- SQLite3 数据库使用
- 使用sqlite3创建数据库
- 使用sqlite3 模块操作sqlite3数据库
- 嵌入式数据库SqLite3使用入门
- listview中radiobutton的选择问题
- UITableViewCell 右侧的小箭头
- linux系统基础知识学习笔记
- 使用shell脚本和系统日志来统计并发数
- 几个重要的数据结构
- Sqlite3 数据库使用
- 通过命令行启动RFT(Rational Functional Tester)
- 如何在Eclipse RCP中使用第三方包
- url 参数带有中文 或十六进制数字 解决方法
- MyEclipse 常用Alt+Shift+s 快捷方式
- VB.NET环境下的Socket编程举例
- 经典UITableView实现
- Modbus通信协议
- 一段神奇的Java代码