【发布时间】:2014-10-31 06:52:10
【问题描述】:
我有 2 个主要问题。
第一个;我在我的 iOS 应用程序中使用了 SQLite 的任何数据库浏览器吗?
第二个问题——大问题——有点复杂。
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"insider.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"create table if not exists userInfo (device_token text primary key, device_type text, carrier text, app_version text,os_version text, first_name text, last_name text,last_viewed_item text, last_added_item text, last_item_price_tag text, name_entered integer, login_screen integer, item_detailed_screen integer )";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
这是我的 createDB 方法,我称之为我的 applicationDIdBecome active,
我有几种方法可以获取将保存到我的数据库中的项目其中之一;
-(BOOL)getUserFirstName:(NSString *)firstName {
NSLog(@"User's first name is this %@",firstName);
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into userInfo (first_name) values (\"%@\")", firstName];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
sqlite3_reset(statement);
}
return NO;
}
我有很多这样的方法(对于我的 create table 语句中的每一列)
我想要的是这个;
我有一个使用 mysql 作为数据库的后端。
我需要创建最后一个插入行并将其设为 JSON 格式,以便我可以对其进行操作并将其写入我的后端服务器。
我该怎么做?
提前致谢。
干杯。
【问题讨论】:
-
如果这是您的第一个 Web 服务项目,我可能建议您查看 Wenderlich 的 How to Write a Simple MySQL Web Service for iOS app 和 How to Write a iOS App that Uses a Web Service。后者使用现在已经过时的 ASIHTTPRequest(现在许多人更喜欢 AFNetworking),但它可能仍然是 iOS 应用程序和后端 MySQL 数据库之间的交配仪式的一个很好的介绍。
标签: ios objective-c json sqlite cocoa-touch