我正在编写一个简单的查询,如下所示
const char *sql = "插入 abc(name) 值 ('Royal')";
这将每次将“皇家”插入到我的“姓名”中,所以现在我想每次都将用户的输入作为酒店的名称,并希望保存它们而不是“皇家”,那我该怎么办?
如果您对我的问题不清楚,您可以再次像我一样,,,,,
我正在编写一个简单的查询,如下所示
const char *sql = "插入 abc(name) 值 ('Royal')";
这将每次将“皇家”插入到我的“姓名”中,所以现在我想每次都将用户的输入作为酒店的名称,并希望保存它们而不是“皇家”,那我该怎么办?
如果您对我的问题不清楚,您可以再次像我一样,,,,,
这段代码非常简单,可以在 sqlite3 表中插入值
-(void)writeValueInSettings:(NSMutableArray *)arrayvalue
{
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
database *objectDatabase=[[database alloc]init];
NSString *stringvalue2=[objectDatabase countValue];
[objectDatabase release];
NSLog(@"opened");
NSString *sql1;
sql1=[[NSString alloc] initWithFormat:@"insert into setting values('%i','%i','%i','%@','%i','%i','%@','%i','%i','%i','%i','%i','%i','%@');",intvalue1,
[[arrayvalue objectAtIndex:0] intValue],[[arrayvalue objectAtIndex:1] intValue],[arrayvalue objectAtIndex:2],[[arrayvalue objectAtIndex:3] intValue],[[arrayvalue objectAtIndex:4]intValue ],[arrayvalue objectAtIndex:5],[[arrayvalue objectAtIndex:6]intValue],[[arrayvalue objectAtIndex:7]intValue ],[[arrayvalue objectAtIndex:8] intValue],[[arrayvalue objectAtIndex:9] intValue],[[arrayvalue objectAtIndex:10]intValue ],[[arrayvalue objectAtIndex:11]intValue],[arrayvalue objectAtIndex:12]];
char *err1;
if (sqlite3_exec(myDatabase,[sql1 UTF8String],NULL,NULL,&err1)==SQLITE_OK)
{
NSLog(@"value inserted:");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Attention" message:@"You inserted successfully" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
[alert show];
[alert release];
}
[sql1 release];
sqlite3_close(myDatabase);
}
}
您需要连接到数据库
- (sqlite3 *)database {
if (nil == db) {
NSString *path = <path to your database>;
int res = sqlite3_open([path UTF8String], &db);
if (res != SQLITE_OK){
// handle the error.
db = nil;
return nil;
}
}
return db;
}
然后你可以用查询来调用它
-(void)executeQuery:(NSString *)query{
sqlite3_stmt *statement;
if (sqlite3_prepare_v2([self database], [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {
sqlite3_step(statement);
}else{
// handle the error.
}
sqlite3_finalize(statement);
}
-(void)dealloc {
//close database connection
sqlite3_close(db);
db = nil;
[super dealloc];
}
嗨,如果您在用户输入文本字段中获取用户输入
NSString *qry=[NSString stringwithformat:@"插入 abc(name) 值 (\"%@\")", userinput.text ]; const char *sql = [qry UTF8string]; sqlite3 *联系数据库; const char *dbpath = [databasePath UTF8String]; // 将 NSString 转换为 UTF-8 if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) { if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) == SQLITE_OK) { // SQL 语句执行成功 } } 别的 { //打开数据库失败 }
-(NSArray *) executeSelect:(NSString *)query {
//Data search block****************************************
char *zErrMsg;
char **result;
int nrow, ncol;
sqlite3_get_table(
[self database], /* An open database */
[query UTF8String], /* SQL to be executed */
&result, /* Result written to a char *[] that this points to */
&nrow, /* Number of result rows written here */
&ncol, /* Number of result columns written here */
&zErrMsg /* Error msg written here */
);
NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:3];
for (int i=0; i<nrow; i++){
[returnArray addObject:[NSString stringWithUTF8String:result[ncol + i]]];
}
sqlite3_free_table(result);
return returnArray;
}