hai我試圖在運行時以編程方式創建sqlite數據庫。任何人都可以說如何在iphone sdk中創建它。以編程方式在iphone sdk中創建sqlite數據庫
回答
只要調用sqlite3_open函數,它將在路徑上沒有數據庫的情況下創建數據庫。
// generate databasePath programmatically
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// your code here
}
後,如果您需要在此
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"if");
}
sqlite3_close(contactDB);
} else
{
NSLog(@"else");
}
}
[filemgr release];
}
-(IBAction)table
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
// if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE LIST (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"tables failed");
// status.text = @"Failed to create table";
}
sqlite3_close(contactDB);
}
else
{
NSLog(@"tables failed");
//status.text = @"Failed to open/create database";
}
}
[filemgr release];
}
導入.m文件#import
進口在.m文件#進口sqlite3.h更多的代碼示例,並在烏拉圭回合項目中添加框架libsqlite3.0.dylib
評論首先創建NSObject類並將其命名爲Database。 在.H類
@interface database : NSObject
{
NSString *databasePath;
NSString *databaseName;
sqlite3 *myDatabase;
NSArray *documentPaths;
NSString *documentsDir;
}
// ---初始方法-------
-(void)createDatabaseIfNeeded;
// --------------- --path find method --------------------- //
-(void)pathFind;
// ------------- ----寫入值---------------------- //
-(void)writeValueInSettings:(NSMutableArray *)arrayvalue;
// -------------------從設定表獲取值------------ //
-(NSMutableArray *)fetchValue;
// -------------------更新值--------------------- //
-(void)updateSetting:(NSArray *)arr;
.M類寫
-(id)init
{
if((self=[super init]))
{
[self createDatabaseIfNeeded];
}
return self;
}
//-----------create database if needed method--------------//
-(void)createDatabaseIfNeeded
{
[self pathFind];
BOOL success;
NSFileManager *filemgr = [NSFileManager defaultManager];
success=[filemgr fileExistsAtPath:databasePath];
if (success)return;
NSLog(@"not success");
//Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[filemgr copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
// ----------------路徑找到----------------- //
-(void)pathFind
{
databaseName = @"accDataBase.DB";
// Get the path to the documents directory and append the databaseName
documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
}
// ------------------寫入設定值---------------- //
-(void)writeValueInSettings:(NSMutableArray *)arrayvalue
{
NSLog(@"%@",arrayvalue);
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
database *objectDatabase=[[database alloc]init];
NSString *stringvalue2=[objectDatabase countValue];
[objectDatabase release];
int intvalue1=[stringvalue2 intValue];
intvalue1=intvalue1+1;
NSLog(@"opened");
NSString *sql1;
sql1=[[NSString alloc] initWithFormat:@"insert into setting values('%i','%i','%i','%@','%i','%i','%@','%i','%i','%i','%i','%i','%i','%@');",intvalue1,
[[arrayvalue objectAtIndex:0] intValue],[[arrayvalue objectAtIndex:1] intValue],[arrayvalue objectAtIndex:2],[[arrayvalue objectAtIndex:3] intValue],[[arrayvalue objectAtIndex:4]intValue ],[arrayvalue objectAtIndex:5],[[arrayvalue objectAtIndex:6]intValue],[[arrayvalue objectAtIndex:7]intValue ],[[arrayvalue objectAtIndex:8] intValue],[[arrayvalue objectAtIndex:9] intValue],[[arrayvalue objectAtIndex:10]intValue ],[[arrayvalue objectAtIndex:11]intValue],[arrayvalue objectAtIndex:12]];
char *err1;
if (sqlite3_exec(myDatabase,[sql1 UTF8String],NULL,NULL,&err1)==SQLITE_OK)
{
NSLog(@"value inserted:");
}
[sql1 release];
sqlite3_close(myDatabase);
}
} // ------------獲取所有值------------- //
-(NSMutableArray *)fetchValue
{
NSMutableArray *list=nil;
list=[[[NSMutableArray alloc]init] autorelease];
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
NSString *sql=[NSString stringWithFormat: @"select * from setting where primaryKey=1"];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(myDatabase, [sql UTF8String], -1,&statement, NULL)==SQLITE_OK)
{
if(sqlite3_step(statement)==SQLITE_ROW)
{
for(int i=0;i<=13;i++)
{
char *pass=(char*)sqlite3_column_text(statement,i);
NSString *msg=[[NSString alloc]initWithUTF8String:pass];
[list addObject:msg];
[msg release];
}
}
sqlite3_finalize(statement);
}
sqlite3_close(myDatabase);
}
return list;
}
// ----- -----------更新設置表方法--------------- //
-(void)updateSetting:(NSArray *)arr
{
if(sqlite3_open([databasePath UTF8String],&myDatabase)==SQLITE_OK)
{
NSLog(@"opened");
sqlite3_stmt *compiledStmt;
// NSLog(@"%@",arr);
NSString *sqlStmt=[NSString stringWithFormat:@"UPDATE setting SET ragular=%i,cycle=%i, flow='%@', hour=%i,minute=%i,formate='%@' ,tenminute=%i ,thirtyminute=%i,sixtymin=%i, twentymin=%i, fourtyfivemin=%i ,other='%@',formatemessage ='%@' WHERE primaryKey=%i;",[[arr objectAtIndex:0]intValue],[[arr objectAtIndex:1]intValue],[arr objectAtIndex:2],[[arr objectAtIndex:3]intValue],[[arr objectAtIndex:4]intValue],[arr objectAtIndex:5],[[arr objectAtIndex:6]intValue],[[arr objectAtIndex:7]intValue],[[arr objectAtIndex:8]intValue],[[arr objectAtIndex:9]intValue],[[arr objectAtIndex:10]intValue],[arr objectAtIndex:11],[arr objectAtIndex:12],1];
// NSLog(@"%@",sqlStmt);
if(sqlite3_prepare_v2(myDatabase, [sqlStmt UTF8String],-1,&compiledStmt, NULL)==SQLITE_OK)
{
NSLog(@"updateding......cycle");
}
sqlite3_step(compiledStmt);
sqlite3_close(myDatabase);
}
}
哇。感謝真的很好的幫助.... – ravinder521986 2013-05-03 06:43:30
- 1. 以編程方式創建數據庫
- 2. 如何以編程方式創建sqlite數據庫?
- 3. 以編程方式編輯SQLite數據庫中的數據
- 4. 如何以編程方式在sqlite數據庫中創建表格
- 5. 如何使用C++在Sqlite中以編程方式創建數據庫?
- 6. iPhone SDK:我如何以編程方式創建正方形?
- 7. 以編程方式創建SQLite表格
- 8. 以編程方式在SQL Server中創建數據庫
- 9. 如何在SQL Server中以編程方式創建數據庫?
- 10. 如何以編程方式在couchdb中創建數據庫?
- 11. 以編程方式在Spring中創建新的數據庫
- 12. 如何使用Xcode以編程方式創建SQLite數據庫中的索引?
- 13. iPhone - 以編程方式創建UITabBar?
- 14. 如何在Qt中以編程方式製作SQLITE數據庫?
- 15. 以編程方式在任意位置創建數據庫
- 16. 準備代碼以編程方式創建SQLite數據庫 - 導出模板
- 17. 以編程方式創建Oracle數據庫和模式ADO.NET
- 18. iPhone SDK:以編程方式提交UIWebview
- 19. iPhone SDK - 以編程方式添加UINavigationController
- 20. 以編程方式更新android中的SQLite數據庫?
- 21. 在nlog中以編程方式創建數據庫以啓用使用DatabaseTarget
- 22. 以C#編程方式從數據庫創建XSD
- 23. 以編程方式創建數據庫(沒有權限?)
- 24. 如何以編程方式創建新的Mongo數據庫?
- 25. Android - 如何以編程方式創建數據庫
- 26. 以編程方式創建SQL數據庫c#
- 27. 如何以編程方式創建Firebird數據庫?
- 28. C#使用SMO以編程方式創建數據庫
- 29. 通過vb.net以編程方式創建Access數據庫
- 30. 在Asp.Net MVC應用程序中以編程方式創建新數據庫?
關於此主題的任何教程,saurabh。請你可以給我們謝謝 – 2011-05-19 06:37:41