//
GroupDBManager.h
#import <Foundation/Foundation.h>
#import "GroupDBmodel.h"
@interface Groupdbmanager:nsobject
This method returns a singleton of Dbmanager
+ (Groupdbmanager *) Sharemanager;
Add a piece of data
-(void) Insertdatawithmodel: (Groupdbmodel *) model;
-(void) Deletemessagewithchatid: (nsstring*) Chatid;
Modify one piece of data in a database
-(void) Updatewithmodel: (Groupdbmodel *) model UUID: (int) uuid;
/** when the update data was created
@pragma createtime time to create information
@pragma status sent (0 not sent 1 sent)
@pragma UUID of information in the UUID database table
*/
-(BOOL) Updatemodelwithcreatetime: (NSString *) createtime andstatus: (NSString *) status anduuid: (int) uuid;
Get all data
-(Nsarray *) Fetchallgroupbulletin;
Get all the data under the corresponding QuestionID
-(Nsarray *) Fetchallhistorywithquestionid: (NSString *) QuestionID;
Gets the maximum time under the corresponding QuestionID
-(NSString *) Findmaxtime: (NSString *) QuestionID;
Gets a new UUID (the maximum value in the UUID)
-(int) findmaxuuid;
Determine if the record exists
-(BOOL) Isexistmodel: (nsstring*) Chatid;
Delete entire table
-(void) deletedbfile;
//
Groupdbmanager.m
#import "GroupDBManager.h"
#import "FMDatabase.h"//third-party open Source Library, which encapsulates the operation of the Apple native Sqlite3 lightweight database
@implementation Groupdbmanager
{
Fmdatabase *_database;
}
A variable modified by the static keyword that performs only one initialization operation, and the variable holds the object that was initialized for the first time.
Staticgroupdbmanager *manager = nil;
+ (Groupdbmanager *) sharemanager{
if (manager = = nil) {
Manager = [[Groupdbmanageralloc] init];
}
Returnmanager;
}
Overriding the Init method
-(ID) init{
Self = [Superinit];
if (self) {
Specify the path created for the database file
NSString *dbpath = [Nshomedirectory () stringbyappendingformat:@ "/documents/groupbulletin.db"];
Initialize the Fmdatabase object and pass the path to the database
_database = [[Fmdatabasealloc] initwithpath:dbpath];
Open two meaning: if the specified path does not have a user.db database file, this file will be created and opened, if the specified path has a database file
If you create and open a successful return Yes
[Selfcreatedbfile];
}
returnself;
}
Create a database file
-(void) createdbfile{
if ([_databaseopen]) {
Create a table with BLOBs representing binary objects
NSString *createsql = @ "CREATE TABLE IF not EXISTS groupbulletin (ID integer primary key autoincrement, chatter varchar (2 ), Chatid varchar (grouptext), Headphoto varchar, sendername varchar (in), Sendertime varchar (256)) ";
Executeupdate Execute SQL statements, create tables, add, delete, change SQL statements are all performed by this method
Execute SQL statement successfully returns YES
BOOL issuccessed =[_databaseexecuteupdate:createsql];
if (!issuccessed) {
Failed to create TABLE, Lasterrormessage get error information
NSLog (@ "error:%@", _database.lasterrormessage);
}
}
}
Add a piece of data
-(void) Insertdatawithmodel: (Groupdbmodel *) model{
UIImage *headimage = model.img;
Convert to NSData (convert PNG-formatted images to NSData, almost lossless conversions)
NSData *data = uiimagepngrepresentation (headimage);
SQL statement, use the? As a placeholder, no matter what type of data
[Self createdbfile];
if ([SelfisExistModel:model.chatId]) {
NSLog (@ "Existence of this article");
Return
}
else {
NSString * Insertsql = @ "INSERT into Groupbulletin (Chatter,chatid,grouptext,headphoto,sendername, SenderTime) VALUES (? ,?,?,?,?,?)";
BOOL issuccessed = [_databaseexecuteupdate:insertsql,model.chatter,model.chatid,model.grouptext,model.headphoto, Model.sendername,model.sendertime];
if (!issuccessed) {
NSLog (@ "inserterror:%@", _database.lasterrormessage);
}
}
}
Delete a piece of data
-(void) Deletemessagewithchatid: (nsstring*) Chatid
{
NSString *[email protected] "delete from groupbulletin where Chatid =?";
BOOL issuccessed = [_databaseexecuteupdate:deletesql,chatid];
if (!issuccessed) {
NSLog (@ "inserterror:%@", _database.lasterrormessage);
}
}
-(BOOL) Isexistmodel: (nsstring*) chatid{
Fmresultset * set = [_databaseexecutequery:@ "select * from Groupbulletin WHERE Chatid =?", [Nsnumbernumberwithint:[chatid Intvalue]];
if ([Set next]) {
Returnyes;
}
else {
Returnno;
}
}
Modify one piece of data in a database
-(void) Updatewithmodel: (Groupdbmodel *) Model ID: (int) id{
//
NSString *updatesql = @ "Update groupbulletin set Chatid =?" WHERE id =? ";
BOOL issuccessed = [_database executeupdate:updatesql,model.chatid,[nsnumber numberwithint:id]];
if (!issuccessed) {
NSLog (@ "Update error:%@", _database.lasterrormessage);
// }
//}
/** when the update data was created
*/
-(BOOL) Updatemodelwithcreatetime: (NSString *) createtime andstatus: (NSString *) status anduuid: (int) uuid{
NSString * Updatesql = @ "UPDATE groupbulletin SET createtime =?, status =?" WHERE uuid =? ";
BOOL ISSUCC = [_databaseexecuteupdate:updatesql,createtime,status,[nsnumbernumberwithint:uuid]];
if (!ISSUCC) {
NSLog (@ "Update error:%@", _database.lasterrormessage);
Returnno;
}
Returnyes;
}
Get all data
-(Nsarray *) fetchallgroupbulletin{
NSString *selectsql = @ "SELECT * from Groupbulletin";
ExecuteQuery The SQL statement used to execute the query
Fmresultset class for storing query results
Fmresultset *set =[_databaseexecutequery:selectsql];
Next method, starting from the first data, take the last one, take the current data return yes, not the return no
Nsmutablearray *array = [Nsmutablearrayarray];
while ([Set next]) {
Groupdbmodel *model = [[Groupdbmodelalloc] init];
Stringforcolumn the value of a field based on the name of the field
Model.chatid = [Set stringforcolumn:@ "Chatid"];
}
return array;
}
Get all the data under the corresponding QuestionID
-(Nsarray *) Fetchallhistorywithquestionid: (NSString *) questionid{
NSString *selectsql = @ "SELECT * from groupbulletin WHERE chatter =?";
ExecuteQuery The SQL statement used to execute the query
Fmresultset class for storing query results
Fmresultset *set =[_databaseexecutequery:selectsql,questionid];
Next method, starting from the first data, take the last one, take the current data return yes, not the return no
Nsmutablearray *array = [Nsmutablearrayarray];
while ([Set next]) {
Groupdbmodel *model = [[Groupdbmodelalloc] init];
Stringforcolumn the value of a field based on the name of the field
Model.chatid = [Set stringforcolumn:@ "Chatid"];
Model.chatter = [Set stringforcolumn:@ "chatter"];
Model.grouptext = [Set stringforcolumn:@ "Grouptext"];
Model.headphoto = [Set stringforcolumn:@ "Headphoto"];
Model.sendername = [Set stringforcolumn:@ "SenderName"];
Model.sendertime = [Set stringforcolumn:@ "Sendertime"];
NSLog (@ "%@----%@----%@----%@----%@----%@", Model.chatid,model.chatter,model.grouptext,model.headphoto, Model.sendername,model.sendertime);
[Array Addobject:model];
}
return array;
}
-(NSString *) Findmaxtime: (NSString *) questionid{
NSString *selectsql = @ "Select MAX (createtime) from gro upbulletin where questionuuid =?";
NSString * selectsql = @ "SELECT * from Groupbulletin WHERE questionuuid =?";
Fmresultset *set =[_databaseexecutequery:selectsql,questionid];
Nsmutablearray * Marray = [[Nsmutablearrayalloc] init];
while ([Set next]) {
NSString * Lasttime = [set stringforcolumnindex:6];
if (lasttime) {
[Marray Addobject:lasttime];
}
}
if (Marray.count > 0) {
NSString * str = marray[0];
for (int i = 1; i < Marray.count; i++) {
if ([Marray[i] compare:str options:0] > 0) {
str = marray[i];
}
}
return str;
}
else {
[email protected] "0";
}
}
-(NSString *) Findmaxtime: (NSString *) questionid{
//
NSString * selectsql = @ "SELECT * from Groupbulletin WHERE questionuuid =?";
//
Fmresultset *set =[_database Executequery:selectsql,questionid];
Nsmutablearray * Marray = [[Nsmutablearray alloc] init];
while ([Set next]) {
NSString * Lasttime = [set stringforcolumnindex:6];
return lasttime;
// }
return nil;
//}
-(int) findmaxuuid{
NSString * selectsql = @ "Select MAX (UUID) from Groupbulletin";
int maxuuid = 0;
Fmresultset * set = [_databaseexecutequery:selectsql];
while ([Set next]) {
Maxuuid = [Set intforcolumn:@ "UUID"];
Maxuuid = [set intforcolumnindex:0];
}
return maxuuid;
}
Delete entire table
-(void) deletedbfile{
NSString *dbpath = [Nshomedirectory () stringbyappendingformat:@ "/documents/groupbulletin.db"];
NSFILEMANAGER * fm = [Nsfilemanagerdefaultmanager];
if ([FM Fileexistsatpath:dbpath]) {
if ([_databaseopen]) {
NSString * Selectsql = @ "DELETE from Groupbulletin";
[_databaseexecuteupdate:selectsql];
[_databaseclose];
}
[FM Removeitematpath:dbpath Error:nil];
}
}
@end
Simple package for IOS SQL