In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the example analysis of sqlite operation and encapsulation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Ocsqlite.h
[plain]
View plaincopy
/ /
/ / OCSqlite.m
/ / sqlite
/ /
/ / Created by fengsh on 12-12-3.
/ / Copyright (c) 2012 sqlite_Lib. All rights reserved.
/ /
/ *
The main design of the encapsulation of SQLITE is the habit of operation. For lightweight data.
For the amount of data, you need to pay attention to the memory cost.
, /
# import
# import
Enum fieldtype
{
FtInt,ftFloat,ftDouble,ftString,ftBlob,ftBool,ftDate,ftTime,ftDateTime,ftBinary
}
/ *
Field class
Function: mainly used to correspond to the field properties in the database
Field name, field type, field value, field index number
, /
@ interface OCField: NSObject
{
NSString* fieldName
Id fieldValue
Enum fieldtype mtype
Int seq_column
}
-(NSString*) toString
-(NSInteger) toInteger
-(NSDate*) toDate
-(NSString*) toDateString
-(NSString*) toTimeString
-(NSString*) toDateTimeString
-(NSNumber*) toNumber
-(enum fieldtype) getFieldType
@ property (nonatomic) int seq_column
@ end
/ *
Dataset class
Function:
A collection similar to a data source with cursors that can access the data in the data source
, /
@ interface OCDataset: NSObject
{
NSMutableArray* records
NSInteger cursor
}
-(void) clear
-(NSInteger) count
-(BOOL) next
-(BOOL) first
-(BOOL) move: (NSInteger) index
-(OCField*) fieldbyname: (NSString*) fieldname
-(OCField*) indexOffield: (NSInteger) index
@ end
/ *
Simple data definition language operation and encapsulation of database query
Parameter binding is not supported, so you also need extension code on processing blob.
Follow-up improvement
, /
@ interface OCSqlite: NSObject
{
Sqlite3* db
OCDataset* dataset
}
-(id) init
-(BOOL) ConnectToDB: (NSString*) dbfilepath
-(void) DisconnectDB
-(BOOL) startTranslation
-(BOOL) commitTranslation
-(BOOL) rollbackTranslation
-(BOOL) excesql: (NSString*) ddlsql
-(BOOL) query: (NSString*) qysql
@ property (nonatomic,readonly) OCDataset* dataset
@ end
Ocsqlite.m
[plain]
View plaincopy
/ /
/ / OCSqlite.m
/ / sqlite
/ /
/ / Created by fengsh on 12-12-3.
/ / Copyright (c) 2012 sqlite_Lib. All rights reserved.
/ /
# import "OCSqlite.h"
@ implementation OCField
@ synthesize seq_column
-(id) init
{
Self = [super init]
If (self) {
FieldValue = NULL
Return self
}
Return nil
}
-(void) setfield: (NSString*) name withvalue: (sqlite3_value*) value withtype: (NSString*) tp
{
FieldName = name
NSString* result = @ ""
If ([tp isEqualToString:@ "integer"] | | [tp isEqualToString:@ "smallint"])
{
Mtype = ftInt
FieldValue = (id) sqlite3_value_int (value)
Return
}
Else if ([tp isEqualToString:@ "boolean"])
{
Mtype = ftBool
}
Else if ([tp isEqualToString:@ "float"])
{
Mtype = ftFloat
}
Else if ([tp isEqualToString:@ "double"] | | [tp isEqualToString:@ "real"])
{
Mtype = ftDouble
}
Else if ([tp isEqualToString:@ "text"] | | [tp isEqualToString:@ "varchar"])
{
Mtype = ftString
}
Else if ([tp isEqualToString:@ "blob"])
{
Mtype = ftBlob
Return
}
Else if ([tp isEqualToString:@ "date"])
{
Mtype = ftDate
}
Else if ([tp isEqualToString:@ "time"])
{
Mtype = ftTime
}
Else if ([tp isEqualToString:@ "timestamp"])
{
Mtype = ftDateTime
}
Else if ([tp isEqualToString:@ "binary"])
{
Mtype = ftBinary
Return
}
Char* floatstr = (char*) sqlite3_value_text (value)
If (floatstr) {
Result = [NSString stringWithCString:floatstr encoding:NSUTF8StringEncoding]
}
FieldValue = result
}
-(NSString*) toString
{
Return (NSString*) fieldValue
}
-(NSInteger) toInteger
{
Return (int) fieldValue
}
-(NSNumber*) toNumber
{
Switch (mtype) {
Case ftFloat:
Return [NSNumber numberWithFloat: [(NSString*) fieldValue floatValue]]
Break
Case ftDouble:
Return [NSNumber numberWithDouble: [(NSString*) fieldValue doubleValue]]
Break
Case ftBool:
Return [NSNumber numberWithBool: [(NSString*) fieldValue boolValue]]
Break
Default:
Return [NSNumber numberWithInt: [(NSString*) fieldValue intValue]]
Break
}
}
-(NSString*) toDateString
{
NSDateFormatter* fmt = [NSDateFormatter alloc] init] autorelease]
[fmt setDateFormat:@ "yyyy-mm-dd"]
NSDate* date = [fmt dateFromString:fieldValue]
NSString* datestr = [fmt stringFromDate:date]
Return (datestr==nil)? @ "": datestr
}
-(NSString*) toTimeString
{
NSDateFormatter* fmt = [NSDateFormatter alloc] init] autorelease]
[fmt setDateFormat:@ "HH:mm:ss"]; / / H is 0 murine 23 h is 1-12
NSDate* time = [fmt dateFromString:fieldValue]
NSString* timestr = [fmt stringFromDate:time]
Return (timestr==nil)? @ "": timestr
}
-(NSString*) toDateTimeString
{
NSDateFormatter* fmt = [NSDateFormatter alloc] init] autorelease]
[fmt setDateFormat:@ "yyyy-MM-dd HH:mm:ss"]; / / H is 0 murine 23 h is 1-12
NSDate* datetime = [fmt dateFromString:fieldValue]
NSString* dtimestr = [fmt stringFromDate:datetime]
Return (dtimestr==nil)? @ "": dtimestr
}
-(NSDate*) toDate
{
NSDateFormatter* fmt = [[NSDateFormatter alloc] init]
[fmt setDateFormat:@ "yyyy-MM-dd HH:mm:ss"]
NSDate* date = [fmt dateFromString:fieldValue]
Return date
}
-(enum fieldtype) getFieldType
{
Return mtype
}
@ end
@ implementation OCDataset
-(id) init
{
Self = [super init]
If (self) {
Cursor =-1
Records = [[NSMutableArray alloc] init]
Return self
}
Return nil
}
-(void) dealloc
{
[records release]
[super dealloc]
}
-(void) reset
{
Cursor = 0
}
-(void) fillData: (sqlite3_stmt*) cmd
{
NSInteger colcount = sqlite3_column_count (cmd)
NSMutableDictionary* fields = [NSMutableDictionary alloc] init] autorelease]
For (NSInteger I = 0; I < colcount; iTunes +) {
Char* fieldname = (char*) sqlite3_column_name (cmd, I)
NSString* strfieldname = [NSString stringWithCString:fieldname encoding:NSUTF8StringEncoding]
Sqlite3_value* mvalue = sqlite3_column_value (cmd, I)
Char* ity = (char*) sqlite3_column_decltype (cmd, I)
NSString* stype = [NSString stringWithCString:ity encoding:NSUTF8StringEncoding]
/ / int type = sqlite3_column_type (cmd, I)
OCField* field = [[OCField alloc] init]
[field setfield:strfieldname withvalue:mvalue withtype:stype]
Field.seq_column = I
[fields setObject:field forKey:strfieldname]
}
[records addObject:fields]
}
-(void) clear
{
[records removeAllObjects]
Cursor =-1
}
-(NSInteger) count
{
Return [records count]
}
-(OCField*) fieldbyname: (NSString*) fieldname
{
NSMutableDictionary* dic = [records objectAtIndex:cursor]
Return (OCField*) [dic objectForKey:fieldname]
}
-(BOOL) next
{
+ + cursor
Int rcount = [records count]
If (cursor 0) {
Cursor = 0
Return YES
}
Else
{
Cursor =-1
Return NO
}
}
-(BOOL) move: (NSInteger) index
{
Int idx = index-1
If (- 1 < idx < [records count]) {
Cursor = idx
Return YES
}
Return NO
}
-(OCField*) indexOffield: (NSInteger) index
{
OCField* ret = nil
Int ct = 0
NSMutableDictionary* dic = [records objectAtIndex:cursor]
For (NSString* key in dic)
{
Ret = [dic objectForKey:key]
If (index = = ct)
Break
Else
Ct++
}
Return ret
}
@ end
@ implementation OCSqlite
@ synthesize dataset
-(id) init
{
Self = [super init]
If (self) {
Dataset = [[OCDataset alloc] init]
Return self
}
Return nil
}
-(void) dealloc
{
[dataset release]
Sqlite3_close (db)
[super dealloc]
}
-(BOOL) ConnectToDB: (NSString*) dbfilepath
{
BOOL successful
Successful = sqlite3_open ([dbfilepath UTF8String], & db) = = SQLITE_OK
If (! successful) {
Sqlite3_close (db)
Return NO
}
Return YES
}
-(void) DisconnectDB
{
Sqlite3_close (db)
}
-(BOOL) excesql: (NSString*) ddlsql
{
Char* err
If (sqlite3_exec (db, [ddlsql UTF8String], NULL, NULL, & err)! = SQLITE_OK)
{
Return NO
}
Return YES
}
-(BOOL) query: (NSString*) qysql
{
Sqlite3_stmt* cmd
If (sqlite3_prepare_v2 (db, [qysql UTF8String],-1, & cmd, nil)! = SQLITE_OK)
{
Return NO
}
[dataset clear]
While (sqlite3_step (cmd) = = SQLITE_ROW)
{
[dataset fillData:cmd]
}
[dataset reset]
Sqlite3_finalize (cmd)
Return YES
}
-(BOOL) startTranslation
{
Char* err
If (sqlite3_exec (db, "begin transaction", NULL, NULL, & err)! = SQLITE_OK)
{
Return NO
}
Return YES
}
-(BOOL) commitTranslation
{
Char* err
If (sqlite3_exec (db, "commit transaction", NULL, NULL, & err)! = SQLITE_OK)
{
Return NO
}
Return YES
}
-(BOOL) rollbackTranslation
{
Char* err
If (sqlite3_exec (db, "rollback transaction", NULL, NULL, & err)! = SQLITE_OK)
{
Return NO
}
Return YES
}
@ end
The above is all the contents of the article "sample Analysis of sqlite Operation and Encapsulation". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.