-
FMDatabase
的使用方法
FMDatabase
的使用方法
-
(NSString*) getPath {
NSArray* paths
=NSSearchPath
ForDirectoriesInDomains(NSDocumentDirector
< br>y,NSUserDomainMask, YES)
return
[[pathsob
jectAtIndex:0]stringByAppendingPathComponent:@
MyTable
1
.
创
建数据库
-(void)CreateTable;{dataBase =
[FMDatabasedatabaseWithPath:[selfgetPath]];
if
(![dataBase open])
NSLog(@
}
[dataBaseexecut
eUpdate:@
MyTable(aa float,bb text,cc
integer,dd integer,ee text)
[dataBaseclose];}
2
.
查询数据
-(void)Que
ryData{//
获取数据
recordArray =
[[NSMutableArrayalloc]init];
db =
[[FMDatabas
ealloc]initWithPath:[selfgetPath]];
if ([db
open]) {
FMResultSet *rs =
[dbexecuteQuery:@
while ([rs
next]){
OneRecord = [[OneRecord
alloc]init];
=
[NSNumbernumberWithFloat:[rs
doubleForColumn:@
= [rs
stringForColumn:@
=
[NSNumbernumberWithInt:[rs
intForColumn:@
=
[NSNumbernumberWithInt:[rs
intForColumn:@
= [rs stringForColumn:@
[recordArrayaddObject: OneRecord];
[OneRecordrelease];
}
[rs close];
[dbclose];}
3
.
更新数据
-(void)UpdateData{if
([dbopen])
{
[dbbeginTransaction];
[dbexecuteUpdate:@
date =
?
[dbexecuteUpdate:@
MyTable
SET bb = ? WHERE date = ?
[dbexecuteUpdate:@
date =
?
[dbexecuteUpdate:@
MyTable
SET dd = ? WHERE date = ?
[dbcommit];
db close];
}
}
4。插入数据
-(void)insertData{//
插入数据库
[dbbeginTransaction];
[dbexecuteUpdate:@
MyTable
(aa,bb,cc,dd,ee) V
ALUES
(?,?,?,?,?)
NSNumber
numberWithFloat:aa],bb,cc,dd,ee];
db
commit];
db
close];
}
5。官方的使用方法为:
UsageThere are three main classes in
FMDB:FMDatabase -
Represents a single
SQLite database. Used for executing SQL
ltSet - Represents the results of
executing a
query on baseQueue - If
you're wanting
to perform queries and
updates on multiple threads, you'll want
to use this class. It's described in
the
se CreationAn FMDatabase is created
with a path
to a SQLite database file.
This path can be one of these three:A
file system path. The file does not
have to exist on disk. If it
does not
exist, it is created for empty string
(@
empty database is created at a
temporary location. This database
is
deleted with theFMDatabase connection is . An
in-memory database is created. This
database will be destroyed
with
theFMDatabase connection is closed.(For more
information on temporary and in-memory
databases, read the
sqlite
documentation on the
subject:/)FMDatabase
*db =
[FMDatabase
databaseWithPath:@
interact
with the database, it must be opened. Opening
fails if
there are insufficient
resources or permissions to open and/or
create the (![db open]) {
[db release];
return;}Executing UpdatesAny sort of
SQL statement which is
not a SELECT
statement qualifies as an update. This includes
CREATE, PRAGMA,UPDATE, INSERT,
ALTER,COMMIT,
BEGIN, DETACH,DELETE,
DROP, END,EXPLAIN,
V
ACUUM,
and REPLACE statements (plus many more).
Basically, if your SQL statement does
not begin withSELECT, it
is an update
ing updates returns a single value,
a
BOOL. A return value of YES means the update was
successfully executed, and a return
value of NO means that
some error was
encountered. If you use the -[FMDatabase
executeUpdate:error:withArgumentsInArray:orV
AList:] method
to execute an
update, you may supply anNSError ** that will be
filled in if execution fails. Otherwise
you may invoke
the-lastErrorMessage
and-lastErrorCode methods to retrieve
more ing QueriesA SELECT statement is a
query and is executed via one of the-
executeQuery...
ing queries returns an
FMResultSet object if
successful, and
nil upon failure. Like executing updates, there is
a variant that accepts anNSError **
parameter. Otherwise you
should use
the-lastErrorMessage and-lastErrorCode methods to
determine why a query order to iterate
through the
-
-
-
-
-
-
-
-
-
上一篇:HP Gen9 在BIOS内升级固件的教程(图文)
下一篇:数据库中英术语