值已插入模拟器但未显示在数据库表中
我为 SQLite 数据库创建了一个插入记录的 insert
语句。我的代码工作正常——当我运行代码时,它显示一条消息,表明已添加新记录。
当我打开SQLite浏览器查看该记录是否已添加到数据库表中时,该记录没有出现。可能是什么问题?我的代码工作正常。它在模拟器中显示联系人添加消息,但在数据库中未添加联系人。
这是我的代码:
#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;
}
这是我的控制器类:
@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];
}
I have created an insert
statement for my SQLite database that inserts records. My code works properly -- when I run my code, it shows a message that a new record was added.
When I open SQLite Browser to see if the record was added to the database table, the record does not appear. What could be the problem? My code is working properly. It shows the contact added message in the simulator, but in the database the contact is not added.
This is my code:
#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 file:
#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;
}
This is my controller class:
@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 file
#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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样尝试
我认为这样你可以做到!祝你好运 !!!
You can try in this way
I think in this way you can do it !!! Good luck !!!
.h 文件
.m 文件
这是您必须插入的方式
如果我帮助您,请不要忘记评价我!祝你好运 !!!
.h file
.m file
Here is how you must insert
If I help you don't forget to rate me !!! Good Luck !!!
添加 NSLog 行并查看日志,然后告诉我您的项目中发生了什么错误。也许这样我可以告诉你该怎么做。
Add NSLog line and see logs after that tell me please what error occurs in your project. Maybe in that way I can tell you what to do.