Iphone 如何以编程方式创建sqlite数据库?
我是iPhone开发新手。我已经使用MacOSX中的终端创建了数据库。现在,我正在尝试使用Iphone 如何以编程方式创建sqlite数据库?,iphone,sqlite,Iphone,Sqlite,我是iPhone开发新手。我已经使用MacOSX中的终端创建了数据库。现在,我正在尝试使用objective-C在iPhone应用程序中以编程方式创建数据库。有人能告诉我一个好的网站,在那里我可以查看已经运行的示例应用程序,并且有学习教程的网站吗?请有人帮帮我。发件人: 如果您想使用数据库进行数据持久化,请查看它是为应用程序存储大量数据的良好资源,请记住,尽管CoreData不是数据库 您可以在iOS平台上非常轻松地使用SQLite,但并没有像使用CoreData那样提供可视化工具来开发它 如果
objective-C
在iPhone应用程序中以编程方式创建数据库。有人能告诉我一个好的网站,在那里我可以查看已经运行的示例应用程序,并且有学习教程的网站吗?请有人帮帮我。发件人:
如果您想使用数据库进行数据持久化,请查看它是为应用程序存储大量数据的良好资源,请记住,尽管CoreData不是数据库 您可以在iOS平台上非常轻松地使用SQLite,但并没有像使用CoreData那样提供可视化工具来开发它 如果您存储的数据量较小,请考虑将信息存储到plist中 如果要存储一些字符串、int或bool,则应使用NSUserDefaults 如果没有更多的信息,我无法确定什么是最适合你的方法 其他资源:
为iPhone应用程序创建数据库有点长,但步骤很多,但非常简单:
首先,您应该下载mozilla fire fox并下载其附加组件,您可以通过单击工具选项找到sqlite数据库选项。您可以创建一个包含列和行的新数据库文件。创建一个数据库文件只是一个简单的过程,之后您可以将该文件包含到您的X代码项目中。
例如:-就像您通常包含在Xcode项目中的图像一样 之后,编写以下代码,这对我来说很好:) 在你的房间里。m文件编写以下sqlite代码
#import "YourHeaderFile"
static sqlite3 *database = nil;
static sqlite3_stmt *addStmt = nil;
static sqlite3_stmt *updateStmt = nil;
@implementation 'YourImplementClass'
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"YourDataBaseFileName.sqlite"];
}
- (void) insertValueToDatabase {
NSString *dbPath =[self getDBPath];
NSString *select_Meal = dummy_select_Meal;
NSString *dateTime = dummy_date_Time;
NSString *location = dummy_location;
NSString *mealItem = dummy_mealItem;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sqlDB = "Insert into iHad(Mealtype,DateAndTime,Location,Mealitem) VALUES (?,?,?,?)";
if(sqlite3_prepare_v2(database, sqlDB, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
sqlite3_bind_text(addStmt, 1, [select_Meal UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [dateTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [location UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [mealItem UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
//else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
//coffeeID = sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
else
sqlite3_close(database);
}
为了节省,你可以创建一个这样的方法
-(void)saveButtonClick
{
if (![dummy_select_Meal length] || ![dummy_mealItem length])
{
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"Select Meal Type/Meal Item"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Ok"];
[alert show];
[alert release];
}
else
{
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30,30,50,50)];
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;//UIActivityIndicatorViewStyleWhiteLarge;//
[self insertValueToDatabase];-----------------------> To insert data into data base
iHadAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
[delegate readMealsFromDatabase];
[rootController reloadTable_SaveButton];
}
[rootController showTablePopupAction:nil];
}
如果您有任何问题,请回复我,我会帮助您:)
检查以下示例:
在didFinishLaunchingWithOptions处调用此函数:
-(void)createdb
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"emp.sqlite"];
NSURL *dbURL = [NSURL fileURLWithPath:dbPath];
// copy a database from the bundle if not present
// on disk
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:dbPath])
{
NSURL *bundlePath = [[NSBundle mainBundle] URLForResource:@"emp" withExtension:@"sqlite"];
NSError *error = nil;
if (!bundlePath || ![fm copyItemAtURL:bundlePath toURL:dbURL error:&error])
{
NSLog(@"error copying database from bundle: %@", error);
}
}
else
{
NSLog(@"Success in Copy file");
}
}
在xcode中创建Sqlite数据库
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self createDb];
return YES;
}
-(void)createDb
{
NSArray *dir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbPath=[NSString stringWithFormat:@"%@/userDb.sqlite",[dir lastObject]];
sqlite3 *db;
NSFileManager *fm=[NSFileManager new];
if([fm fileExistsAtPath:dbPath isDirectory:nil])
{
NSLog(@"Database already exists..");
return;
}
if (sqlite3_open([dbPath UTF8String],&db)==SQLITE_OK)
{
const char *query="create table user (userName VARCHAR(20),userAdd VARCHAR(20), userPass VARCHAR(20))";
if (sqlite3_exec(db, query, NULL, NULL, NULL)==SQLITE_OK)
{
NSLog(@"User table created successfully..");
}else
{
NSLog(@"User table creation failed..");
}
}else
{
NSLog(@"Open table failed..");
}
sqlite3_close(db);
}
在memmory警告后调用此函数 -(iAction)保存数据:(UIButton*)发送方{
}我建议你在谷歌上搜索。搜索引擎不应出现堆栈溢出。由于您是堆栈溢出新手,所以我没有标记向下投票。请先阅读
dbpath==databasePath
<代码>联系人数据库声明在哪里?
-(void)createdb
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"emp.sqlite"];
NSURL *dbURL = [NSURL fileURLWithPath:dbPath];
// copy a database from the bundle if not present
// on disk
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:dbPath])
{
NSURL *bundlePath = [[NSBundle mainBundle] URLForResource:@"emp" withExtension:@"sqlite"];
NSError *error = nil;
if (!bundlePath || ![fm copyItemAtURL:bundlePath toURL:dbURL error:&error])
{
NSLog(@"error copying database from bundle: %@", error);
}
}
else
{
NSLog(@"Success in Copy file");
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self createDb];
return YES;
}
-(void)createDb
{
NSArray *dir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dbPath=[NSString stringWithFormat:@"%@/userDb.sqlite",[dir lastObject]];
sqlite3 *db;
NSFileManager *fm=[NSFileManager new];
if([fm fileExistsAtPath:dbPath isDirectory:nil])
{
NSLog(@"Database already exists..");
return;
}
if (sqlite3_open([dbPath UTF8String],&db)==SQLITE_OK)
{
const char *query="create table user (userName VARCHAR(20),userAdd VARCHAR(20), userPass VARCHAR(20))";
if (sqlite3_exec(db, query, NULL, NULL, NULL)==SQLITE_OK)
{
NSLog(@"User table created successfully..");
}else
{
NSLog(@"User table creation failed..");
}
}else
{
NSLog(@"Open table failed..");
}
sqlite3_close(db);
}
sqlite3_stmt *statement;
const char *dbPath = [databasePath UTF8String];
if(sqlite3_open(dbPath, &contactDB)== SQLITE_OK){
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS(NAME,ADDRESS,PHONE)VALUES(\"%@\",\"%@\",\"%@\")",_name.text,_address.text,_phoneNo.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement)== SQLITE_DONE) {
_status.text = @"Contact added";
_name.text = @"";
_address.text = @"";
_phoneNo.text = @"";
}else{
_status.text = @"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}