gpt4 book ai didi

ios - 在sqlite数据库中插入多个表数据

转载 作者:行者123 更新时间:2023-11-29 00:53:35 25 4
gpt4 key购买 nike

我在数据库中创建了多个表。现在我想将数据插入到这些表中。如何插入多个表数据任何人都可以提供帮助。

我编写了以下代码来创建 1 个表:

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO ATRG (id, name, language,imgurl) VALUES ( \"%@\",\"%@\",\"%@\",\"%@\")", ID, name, lang,imgUrl]; 
const char *insert_stmt = [insertSQL UTF8String]; sqlite3_prepare_v2(_globalDataBase, insert_stmt, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Record Inserted");

} else { NSLog(@"Failed to Insert Record");
}

最佳答案

尝试一下,希望对您有所帮助!!这是我的插入数据代码

#import "Sqlitedatabase.h"
@implementation Sqlitedatabase
+(NSString* )getDatabasePath
{
NSString *docsDir;
NSArray *dirPaths;
sqlite3 *DB;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];

NSString *databasePath = [docsDir stringByAppendingPathComponent:@"myUser.db"];

NSFileManager *filemgr = [[NSFileManager alloc]init];

if ([filemgr fileExistsAtPath:databasePath]==NO) {

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath,&DB)==SQLITE_OK) {

char *errorMessage;
const char *sql_statement = "CREATE TABLE IF NOT EXISTS users(ID INTEGER PRIMARY KEY AUTOINCREMENT,FIRSTNAME TEXT,LASTNAME TEXT,EMAILID TEXT,PASSWORD TEXT,BIRTHDATE DATE)";
if (sqlite3_exec(DB,sql_statement,NULL,NULL,&errorMessage)!=SQLITE_OK) {

NSLog(@"Failed to create the table");
}
sqlite3_close(DB);
}
else{
NSLog(@"Failded to open/create the table");
}
}
NSLog(@"database path=%@",databasePath);
return databasePath;
}

+(NSString*)encodedString:(const unsigned char *)ch
{
NSString *retStr;
if(ch == nil)
retStr = @"";
else
retStr = [NSString stringWithCString:(char*)ch encoding:NSUTF8StringEncoding];
return retStr;
}
+(BOOL)executeScalarQuery:(NSString*)str{

NSLog(@"executeScalarQuery is called =%@",str);

sqlite3_stmt *statement= nil;
sqlite3 *database;
BOOL fRet = NO;
NSString *strPath = [self getDatabasePath];
if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) {
if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_DONE)
fRet =YES;
}

sqlite3_finalize(statement);
}
sqlite3_close(database);
return fRet;
}

+(NSMutableArray *)executeQuery:(NSString*)str{

sqlite3_stmt *statement= nil; // fetch data from table
sqlite3 *database;
NSString *strPath = [self getDatabasePath];
NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) {
if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) {


while (sqlite3_step(statement) == SQLITE_ROW) {
NSInteger i = 0;
NSInteger iColumnCount = sqlite3_column_count(statement);
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
while (i< iColumnCount) {
NSString *str = [self encodedString:(const unsigned char*)sqlite3_column_text(statement, (int)i)];


NSString *strFieldName = [self encodedString:(const unsigned char*)sqlite3_column_name(statement, (int)i)];

[dict setObject:str forKey:strFieldName];
i++;
}

[allDataArray addObject:dict];
}
}

sqlite3_finalize(statement);
}
sqlite3_close(database);
return allDataArray;
}
@end

然后在你想使用的地方调用那个方法!!

NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO users(firstname,lastname,emailid,password,birthdate) VALUES ('%@','%@','%@','%@','%@')",_firstNameTextField.text,_lastNameTextField.text,_emailTextField.text,_passwordTextField.text,_BirthdayTextField.text];
if ([Sqlitedatabase executeScalarQuery:insertSql]==YES)
{

[self showUIalertWithMessage:@"Registration succesfully created"];

}else{
NSLog(@"Data not inserted successfully");
}

如果你想从表中获取数据,那么你可以这样做!!

 NSString *insertSql = [NSString stringWithFormat:@"select emailid,password from users where emailid ='%@' and password = '%@'",[_usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],[_passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];

NSMutableArray *data =[Sqlitedatabase executeQuery:insertSql];
NSLog(@"Fetch data from database is=%@",data);

多次执行查询!!

NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO users (firstname,lastname,emailid,password,birthdate) VALUES ('%@','%@','%@','%@','%@')",_firstNameTextField.text,_lastNameTextField.text,_emailTextField.text,_passwordTextField.text,_BirthdayTextField.text];

NSString *insertSql1 = [NSString stringWithFormat:@"INSERT INTO contact (firstname,lastname,emailid,password,birthdate) VALUES ('%@','%@','%@','%@','%@')",_firstNameTextField.text,_lastNameTextField.text,_emailTextField.text,_passwordTextField.text,_BirthdayTextField.text];

NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:insertSql,insertSql1,nil];

for (int i=0; i<array.count; i++)
{
[Sqlitedatabase executeScalarQuery:[array objectAtIndex:i]];
}

关于ios - 在sqlite数据库中插入多个表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com