Case study of adding, deleting, modifying, and querying IOS Sqlite user interface

Source: Internet
Author: User
Tags password protection

Case study of adding, deleting, modifying, and querying IOS Sqlite user interface
1. case Description: The SQLite operation is encapsulated to convert the operation on the data table into an object, and the user table is added, deleted, modified, and queried through the UI, shows.
2. project directory

The UserModel class is the user object model and corresponds to the fields in the database table. The BaseDB class is a simple encapsulation of sqlite and abstracts three methods for database operations. For details, refer to examples.

//// UserDB. h // SqliteDemo /// Created by Zhao Chao on 14-8-27. // Copyright (c) 2014 Zhao Chao. all rights reserved. // # import "BaseDB. h "# import" UserModel. h "@ interface UserDB: BaseDB {}// Singleton + (id) Creating UserDB; // create the User table-(void) creatTableWithDataBaseName :( NSString *) dbName; // Add a UserModel-(BOOL) addUser :( UserModel *) userModel dbName :( NSString *) dbName; // modify a UserModel-(BOOL) updateUser :( UserModel *) userModel dbName :( NSString *) dbName; // query all-(NSArray *) findAllUser :( NSString *) dbName; // delete an object-(BOOL) deleteUser :( UserModel *) userModel dbName :( NSString *) dbName; @ end
UserDB. m
//// UserDB. m // SqliteDemo /// Created by Zhao Chao on 14-8-27. // Copyright (c) 2014 Zhao Chao. all rights reserved. // # import "UserDB. h "@ implementation UserDBstatic UserDB * db; + (id) login UserDB {if (db = nil) {db = [[UserDB alloc] init];} return db ;} -(void) creatTableWithDataBaseName :( NSString *) dbName {NSString * SQL = @ "create table user (userName text primary key, passWord text, userEmail text)"; [self createTab Le: SQL dataBaseName: dbName];}-(BOOL) deleteUser :( UserModel *) userModel dbName :( NSString *) dbName {NSString * SQL = @ "delete from user where userName =? "; NSArray * params = @ [userModel. userName]; return [self execSql: SQL parmas: params dataBaseName: dbName];}-(BOOL) addUser :( UserModel *) userModel dbName :( NSString *) dbName {NSString * SQL = @ "insert into user (userName, passWord, userEmail) values (?,?,?) "; NSArray * params = @ [userModel. userName, userModel. passWord, userModel. userEmail]; return [self execSql: SQL parmas: params dataBaseName: dbName];}-(NSArray *) findAllUser :( NSString *) dbName {NSString * SQL = @ "select userName, passWord, userEmail from user "; NSArray * result = [self selectSql: SQL parmas: nil dataBaseName: dbName]; NSMutableArray * users = [NSMutableArray array]; for (NSDictionary * dic in result ){ UserModel * user = [[UserModel alloc] init]; user. userName = [dic objectForKey: @ "userName"]; user. passWord = [dic objectForKey: @ "passWord"]; user. userEmail = [dic objectForKey: @ "userEmail"]; [users addObject: user];} return users ;}- (BOOL) updateUser :( UserModel *) userModel dbName :( NSString *) dbName {NSString * SQL = @ "update user set userName = ?, PassWord = ?, UserEmail =? Where userName =? "; NSArray * params = @ [userModel. userName, userModel. passWord, userModel. userEmail, userModel. userName]; return [self execSql: SQL parmas: params dataBaseName: dbName];} @ end

MainViewController is the complete project code download http://download.csdn.net/detail/whzhaochao/7829829 for displaying all users by users AddViewController for adding and modifying user interfaces












C # NET, how does a program connect to the SQLITE database? And can I add, delete, modify, and query tasks?

Download ADO. NET2.0 Provider for SQLite first. Download binaries zip. After downloading and decompress the package, you can find System. Data. SQLite. DLL in the bin directory. Use the Add Reference function in vs2008 to Add System. Data. SQLite. DLL to the project. Run the following code:
String datasource = "e:/tmp/test. db ";
System. Data. SQLite. SQLiteConnection. CreateFile (datasource );
// Connect to the database
System. Data. SQLite. SQLiteConnection conn = new System. Data. SQLite. SQLiteConnection ();
System. Data. SQLite. SQLiteConnectionStringBuilder connstr = new System. Data. SQLite. SQLiteConnectionStringBuilder ();
Connstr. DataSource = datasource;
Connstr. Password = "admin"; // set the Password. SQLite ADO. NET implements database Password protection.
Conn. ConnectionString = connstr. ToString ();
Conn. Open ();
// Create a table
System. Data. SQLite. SQLiteCommand cmd = new System. Data. SQLite. SQLiteCommand ();
String SQL = "CREATE TABLE test (username varchar (20), password varchar (20 ))";
Cmd. CommandText = SQL;
Cmd. Connection = conn;
Cmd. ExecuteNonQuery ();
// Insert data
SQL = "insert into test VALUES ('A', 'B ')";
Cmd. CommandText = SQL;
Cmd. ExecuteNonQuery ();
// Retrieve data
SQL = "SELECT * FROM test ";
Cmd. CommandText = SQL;
System. Data. SQLite. SQLiteDataReader reader = cmd. ExecuteReader ();
StringBuilder sb = new StringBuilder ();
While (reader. Read ())
{
Sb. Append ("username:"). Append (reader. GetString (0). Append ("\ n ")
. Append ("password:"). Append (reader. GetString (1 ));
}
MessageBox. Show (sb. ToString ();... the remaining full text>

Dao implementation class (adding, deleting, modifying, and querying databases) is good in singleton Mode

I think it depends on the situation. DAO itself can use the singleton mode, and there are successful cases. For example, Spring DAO can be used as Singleton in a certain range.
However, it should be noted that the Connection used should be different each time the Singleton is called, because it is necessary to ensure the correctness of the transaction and you can pay attention to this point.

I think there is no problem, because the singleton mode is suitable for modeling stateless services. DAO itself is stateless, so it is suitable for the singleton mode. DriverManager is used every time a Connection is obtained. if getConnection () is obtained, a new Connection is used. Therefore, the same Connection is not used between two things, and the transaction security can be ensured, so there should be no problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.