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

发布于 2024-11-01 17:11:13 字数 3804 浏览 0 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-11-08 17:11:13

你可以这样尝试

   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;
}

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

You can try in this way

   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;
}

I think in this way you can do it !!! Good luck !!!

独守阴晴ぅ圆缺 2024-11-08 17:11:13

.h 文件

#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface SqliteDatabase : NSObject {


}

// Check and Create Sqlite Table.
+ (void) CheckAndCreateDatabase;
+ (void) insert;

@end

.m 文件

// Static Variables.
static NSString* dataBaseName;
static NSString* dataBasePath;
static sqlite3*  database;


#pragma mark -
#pragma mark Check and Create Sqlite Table.
+ (void) CheckAndCreateDatabase {
    NSLog(@"\nCall of: CheckAndCreateDatabase");
    // Set database name.
    dataBaseName = @"Database.db";
    // Initialize database path.
    NSArray* paths         = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDir = [paths objectAtIndex:0];
    dataBasePath           = [NSString stringWithFormat:@"%@/%@", documentsDir, dataBaseName];
    NSLog(@"Database Path is: %@", dataBasePath);
    NSLog(@"Database Name is: %@", dataBaseName);

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager* fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem.
    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 [TableName] (textfieldm, text, textfieldn text)";
            if( sqlite3_exec(database, articlesTable, NULL, NULL, &errorMsg) != SQLITE_OK )
                NSLog( @"Fail to create [TableName] table. Error is: %@", errorMsg );


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

这是您必须插入的方式

+ (void) insert {
    // Create Pre Sqlite query string.
    NSString* preSqliteQuery = @"INSERT INTO [TableName] (textfieldm, textfieldn VALUES ('%@', '%@')";

    // Create Sqlite query string.
    NSString* queryString = [NSString stringWithFormat:preSqliteQuery, Gunameq, Gpassq];
    // Execute query.
    sqlite3_exec(database, [queryString UTF8String], NULL, NULL, NULL);
}

如果我帮助您,请不要忘记评价我!祝你好运 !!!

.h file

#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface SqliteDatabase : NSObject {


}

// Check and Create Sqlite Table.
+ (void) CheckAndCreateDatabase;
+ (void) insert;

@end

.m file

// Static Variables.
static NSString* dataBaseName;
static NSString* dataBasePath;
static sqlite3*  database;


#pragma mark -
#pragma mark Check and Create Sqlite Table.
+ (void) CheckAndCreateDatabase {
    NSLog(@"\nCall of: CheckAndCreateDatabase");
    // Set database name.
    dataBaseName = @"Database.db";
    // Initialize database path.
    NSArray* paths         = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDir = [paths objectAtIndex:0];
    dataBasePath           = [NSString stringWithFormat:@"%@/%@", documentsDir, dataBaseName];
    NSLog(@"Database Path is: %@", dataBasePath);
    NSLog(@"Database Name is: %@", dataBaseName);

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager* fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem.
    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 [TableName] (textfieldm, text, textfieldn text)";
            if( sqlite3_exec(database, articlesTable, NULL, NULL, &errorMsg) != SQLITE_OK )
                NSLog( @"Fail to create [TableName] table. Error is: %@", errorMsg );


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

Here is how you must insert

+ (void) insert {
    // Create Pre Sqlite query string.
    NSString* preSqliteQuery = @"INSERT INTO [TableName] (textfieldm, textfieldn VALUES ('%@', '%@')";

    // Create Sqlite query string.
    NSString* queryString = [NSString stringWithFormat:preSqliteQuery, Gunameq, Gpassq];
    // Execute query.
    sqlite3_exec(database, [queryString UTF8String], NULL, NULL, NULL);
}

If I help you don't forget to rate me !!! Good Luck !!!

冷清清 2024-11-08 17:11:13
sqlite3_exec(database, [queryString UTF8String], NULL, NULL, NULL);
NSLog("Error is: %@");

添加 NSLog 行并查看日志,然后告诉我您的项目中发生了什么错误。也许这样我可以告诉你该怎么做。

sqlite3_exec(database, [queryString UTF8String], NULL, NULL, NULL);
NSLog("Error is: %@");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文