gpt4 book ai didi

objective-c - 值插入模拟器但未显示在数据库表中

转载 作者:行者123 更新时间:2023-11-28 17:46:29 25 4
gpt4 key购买 nike

我为插入记录的 SQLite 数据库创建了一个 insert 语句。我的代码运行正常——当我运行我的代码时,它会显示一条消息,表明添加了一条新记录。

当我打开SQLite Browser查看记录是否添加到数据库表中时,没有出现记录。可能是什么问题呢?我的代码工作正常。它在模拟器中显示联系人已添加消息,但在数据库中未添加联系人。

这是我的代码:

#import <UIKit/UIKit.h>
#import "TAddNewJourney.h"

@interface insertAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
TAddNewJourney *iC;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) TAddNewJourney *iC;

-(void) chekAndCreateDatabase;
-(void)insert;
@end

.m文件:

#import "insertAppDelegate.h"
#import <sqlite3.h>
#import "TAddNewJourney.h"

#import "Global.h"

@implementation insertAppDelegate

@synthesize window;
@synthesize iC;

static NSString* databaseName;
static NSString* databasePath;
static sqlite3* database;





-(void) chekAndCreateDatabase
{
BOOL success;
databaseName=@"demo.sqlite";
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
databasePath = [NSString stringWithFormat:@"%@/%@", documentsDir, databaseName];
NSLog(@"Database Path is: %@", databasePath);
NSLog(@"Database Name is: %@", databaseName);

NSFileManager *fileManager=[NSFileManager defaultManager];
if( [fileManager fileExistsAtPath:databasePath] == NO ) {
// Open database.
if( sqlite3_open( [databasePath UTF8String], &database ) == SQLITE_OK ) {
char* errorMsg;
// Create Articles Table.
const char* articlesTable = "CREATE TABLE [UserJourney] (name text, location text)";
if( sqlite3_exec(database, articlesTable, NULL, NULL, &errorMsg) != SQLITE_OK )
NSLog( @"Fail to create UserJourney table. Error is: %@", errorMsg );


} else NSLog(@"Fail to Create/Open database");
}
else {
NSLog(@"Open database.");
sqlite3_open( [databasePath UTF8String], &database );
}
}



-(void)insert{
[self chekAndCreateDatabase];

// Create Pre Sqlite query string.
NSString* preSqliteQuery = @"INSERT INTO UserJourney VALUES ('%@', '%@')";

// Create Sqlite query string.
NSString* queryString = [NSString stringWithFormat:preSqliteQuery,Gjourney,Glocation];
// Execute query.
if(sqlite3_exec(database, [queryString UTF8String], NULL, NULL, NULL)== SQLITE_DONE){

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"not Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}

这是我的 Controller 类:

@interface TAddNewJourney : UIViewController {

IBOutlet UITextField *mTextJourney;
IBOutlet UITextField *mTextLocation;
IBOutlet UIButton *mStart;
IBOutlet UIButton *mCancel;

}

@property (nonatomic,retain)UITextField *textjourney;
@property (nonatomic,retain)UITextField *textlocation;

-(IBAction)Start:(id)sender;
-(IBAction)Cancel:(id)sender;

@end

.m文件

#import "TAddNewJourney.h"
#import "Global.h"
#import <sqlite3.h>
#import "insertAppDelegate.h"


@implementation TAddNewJourney
@synthesize textjourney=mTextJourney;
@synthesize textlocation =mTextLocation;

-(IBAction)Start:(id)sender{
Gjourney = mTextJourney.text;
Glocation = mTextLocation.text;

insertAppDelegate *app=(insertAppDelegate *)[[UIApplication sharedApplication]delegate];
[app insert];

}

最佳答案

你可以这样试试

   NSString* str = [NSString stringwithformat:@"INSERT OR REPLACE INTO insert (textfieldm, textfieldn) VALUES ('%@', '%@')", Gunameq, Gpassq];
if(sqlite3_exec(database, [str UTF8String], NULL, NULL, NULL) != SQLITE_DONE ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"not Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}

我认为这样你可以做到!!!祝你好运!!!

关于objective-c - 值插入模拟器但未显示在数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5701539/

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