#import "ViewController.h"
#import <sqlite3.h>
#define DBNAME @"shebeiID.db"
#define NAME @"name"
#define NUM @"number"
#define IDNAME @"ID"
#define TABLENAME @"PERSONINFO"
@interface ViewController ()@end@implementation ViewController
@synthesize text1,text2;
- (void)viewDidLoad {
[super viewDidLoad];
//建立表(程序第一次运行时建立)
NSLog(@"沙盒路径:%@",NSHomeDirectory());
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
if (sqlite3_open([database_path UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"数据库打开失败");
}
NSString *sqlCreateTable = @"CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, numeber INTEGER,name TEXT)";
[self execSql:sqlCreateTable];
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)execSql:(NSString *)sql
{
char *err;
if (sqlite3_exec(database, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"数据库操作数据失败!");
}
}
- (IBAction)clickbtn:(id)sender {
[self insertdata];
}
-(void)insertdata{
char *update = "INSERT OR REPLACE INTO PERSONINFO(number,name)""VALUES(?,?);"
char *errorMsg = NULL;
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
//【插入数据】在这里我们使用绑定数据的方法,参数一:sqlite3_stmt,参数二:插入列号,参数三:插入的数据,参数四:数据长度(-1代表全部),参数五:是否需要回调
sqlite3_bind_int(stmt, 1, [self.text1.text intValue]);
sqlite3_bind_text(stmt, 2, [self.text2.text UTF8String], -1, NULL);
}
if (sqlite3_step(stmt) != SQLITE_DONE)
NSLog(@"数据更新失败");
NSAssert(0, @"error updating :%s",errorMsg);
sqlite3_finalize(stmt);
sqlite3_close(database);
}@end
我定义了两个textfield,分别是text1和text2,新建了一个数据库,点击按钮事件,把两个文本框的内容存到数据库中,但是每次都报错,如下面所示的:
2015-12-02 21:03:52.160 sqlite[2670:73495] 沙盒路径:/Users/lisongcun/Library/Developer/CoreSimulator/Devices/17E06407-E9F9-42A1-B4FD-BA1B430BB027/data/Containers/Data/Application/4A657228-8799-450F-80A7-369F1BF64EB6
2015-12-02 21:03:56.245 sqlite[2670:73495] 数据更新失败
2015-12-02 21:03:56.246 sqlite[2670:73495] *** Assertion failure in -[ViewController insertdata], /Applications/程序代码/sqlite/sqlite/ViewController.m:84
2015-12-02 21:03:56.250 sqlite[2670:73495] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'error updating :(null)'
*** First throw call stack:
(
0 CoreFoundation 0x00000001025d5f65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010204fdeb objc_exception_throw + 48
2 CoreFoundation 0x00000001025d5dca +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000101c9dae2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 sqlite 0x0000000101a430ff -[ViewController insertdata] + 959
5 sqlite 0x0000000101a42d2a -[ViewController clickbtn:] + 58
6 UIKit 0x00000001029841fa -[UIApplication sendAction:to:from:forEvent:] + 92
7 UIKit 0x0000000102ae8504 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x0000000102ae87d0 -[UIControl _sendActionsForEvents:withEvent:] + 311
9 UIKit 0x0000000102ae7906 -[UIControl touchesEnded:withEvent:] + 601
10 UIKit 0x00000001029eeaa3 -[UIWindow _sendTouchesForEvent:] + 835
11 UIKit 0x00000001029ef691 -[UIWindow sendEvent:] + 865
12 UIKit 0x00000001029a1752 -[UIApplication sendEvent:] + 263
13 UIKit 0x000000010297cfcc _UIApplicationHandleEventQueue + 6693
14 CoreFoundation 0x00000001025020a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x00000001024f7fcc __CFRunLoopDoSources0 + 556
16 CoreFoundation 0x00000001024f7483 __CFRunLoopRun + 867
17 CoreFoundation 0x00000001024f6e98 CFRunLoopRunSpecific + 488
18 GraphicsServices 0x0000000105bc2ad2 GSEventRunModal + 161
19 UIKit 0x0000000102982676 UIApplicationMain + 171
20 sqlite 0x0000000101a4375f main + 111
21 libdyld.dylib 0x0000000103f1992d start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
本人实在找不出问题了,求教各位大神帮忙,谢谢了啊。
#import <sqlite3.h>
#define DBNAME @"shebeiID.db"
#define NAME @"name"
#define NUM @"number"
#define IDNAME @"ID"
#define TABLENAME @"PERSONINFO"
@interface ViewController ()@end@implementation ViewController
@synthesize text1,text2;
- (void)viewDidLoad {
[super viewDidLoad];
//建立表(程序第一次运行时建立)
NSLog(@"沙盒路径:%@",NSHomeDirectory());
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
if (sqlite3_open([database_path UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"数据库打开失败");
}
NSString *sqlCreateTable = @"CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, numeber INTEGER,name TEXT)";
[self execSql:sqlCreateTable];
}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)execSql:(NSString *)sql
{
char *err;
if (sqlite3_exec(database, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"数据库操作数据失败!");
}
}
- (IBAction)clickbtn:(id)sender {
[self insertdata];
}
-(void)insertdata{
char *update = "INSERT OR REPLACE INTO PERSONINFO(number,name)""VALUES(?,?);"
char *errorMsg = NULL;
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
//【插入数据】在这里我们使用绑定数据的方法,参数一:sqlite3_stmt,参数二:插入列号,参数三:插入的数据,参数四:数据长度(-1代表全部),参数五:是否需要回调
sqlite3_bind_int(stmt, 1, [self.text1.text intValue]);
sqlite3_bind_text(stmt, 2, [self.text2.text UTF8String], -1, NULL);
}
if (sqlite3_step(stmt) != SQLITE_DONE)
NSLog(@"数据更新失败");
NSAssert(0, @"error updating :%s",errorMsg);
sqlite3_finalize(stmt);
sqlite3_close(database);
}@end
我定义了两个textfield,分别是text1和text2,新建了一个数据库,点击按钮事件,把两个文本框的内容存到数据库中,但是每次都报错,如下面所示的:
2015-12-02 21:03:52.160 sqlite[2670:73495] 沙盒路径:/Users/lisongcun/Library/Developer/CoreSimulator/Devices/17E06407-E9F9-42A1-B4FD-BA1B430BB027/data/Containers/Data/Application/4A657228-8799-450F-80A7-369F1BF64EB6
2015-12-02 21:03:56.245 sqlite[2670:73495] 数据更新失败
2015-12-02 21:03:56.246 sqlite[2670:73495] *** Assertion failure in -[ViewController insertdata], /Applications/程序代码/sqlite/sqlite/ViewController.m:84
2015-12-02 21:03:56.250 sqlite[2670:73495] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'error updating :(null)'
*** First throw call stack:
(
0 CoreFoundation 0x00000001025d5f65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010204fdeb objc_exception_throw + 48
2 CoreFoundation 0x00000001025d5dca +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000101c9dae2 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 sqlite 0x0000000101a430ff -[ViewController insertdata] + 959
5 sqlite 0x0000000101a42d2a -[ViewController clickbtn:] + 58
6 UIKit 0x00000001029841fa -[UIApplication sendAction:to:from:forEvent:] + 92
7 UIKit 0x0000000102ae8504 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x0000000102ae87d0 -[UIControl _sendActionsForEvents:withEvent:] + 311
9 UIKit 0x0000000102ae7906 -[UIControl touchesEnded:withEvent:] + 601
10 UIKit 0x00000001029eeaa3 -[UIWindow _sendTouchesForEvent:] + 835
11 UIKit 0x00000001029ef691 -[UIWindow sendEvent:] + 865
12 UIKit 0x00000001029a1752 -[UIApplication sendEvent:] + 263
13 UIKit 0x000000010297cfcc _UIApplicationHandleEventQueue + 6693
14 CoreFoundation 0x00000001025020a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x00000001024f7fcc __CFRunLoopDoSources0 + 556
16 CoreFoundation 0x00000001024f7483 __CFRunLoopRun + 867
17 CoreFoundation 0x00000001024f6e98 CFRunLoopRunSpecific + 488
18 GraphicsServices 0x0000000105bc2ad2 GSEventRunModal + 161
19 UIKit 0x0000000102982676 UIApplicationMain + 171
20 sqlite 0x0000000101a4375f main + 111
21 libdyld.dylib 0x0000000103f1992d start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
本人实在找不出问题了,求教各位大神帮忙,谢谢了啊。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货