public
Description: A simple Nu/Objective-C interface to MySQL databases.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/numysql.git
Added a new method to insert rows in tables.
timburks (author)
Fri May 09 21:44:48 -0700 2008
commit  26b1537bd617113bd6ae8e14c04ddaac92400bec
tree    8a53ae5fc752186f688551de1e2f791f865152ef
parent  63f9682644de897f00b9deac64a638054505ebf5
...
192
193
194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
0
@@ -192,4 +192,33 @@ limitations under the License.
0
     return [self query:command];
0
 }
0
 
0
+- (MySQLResult *) insertRowInTable:(NSString *) tableName withDictionary:(NSDictionary *) dictionary
0
+{
0
+ NSMutableString *command = [NSMutableString stringWithFormat:@"insert into %@ (", tableName];
0
+ NSEnumerator *keyEnumerator = [[dictionary allKeys] objectEnumerator];
0
+ NSObject *key;
0
+ bool first = YES;
0
+ while ((key = [keyEnumerator nextObject])) {
0
+ if (!first)
0
+ [command appendString:@", "];
0
+ first = NO;
0
+ [command appendString:key];
0
+ }
0
+ [command appendString:@") values ("];
0
+ keyEnumerator = [[dictionary allKeys] objectEnumerator];
0
+ first = YES;
0
+ while ((key = [keyEnumerator nextObject])) {
0
+ if (!first)
0
+ [command appendString:@", "];
0
+ first = NO;
0
+ id value = [dictionary objectForKey:key];
0
+ char *escapedValue = (char *) malloc ((1 + 2 * [value length]) * sizeof(char));
0
+ mysql_escape_string(escapedValue, (const char *)[value cStringUsingEncoding:NSUTF8StringEncoding], [value length]);
0
+ [command appendFormat:@"'%s'", escapedValue];
0
+ free(escapedValue);
0
+ }
0
+ [command appendString:@")"];
0
+ return [self query:command];
0
+}
0
+
0
 @end
...
72
73
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
76
77
...
102
103
104
105
 
106
107
 
...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
...
123
124
125
 
126
127
128
129
0
@@ -72,6 +72,27 @@ END))
0
         
0
         (set result (m query:"drop database NuMySQLTest")))
0
      
0
+ (- testInsert is
0
+ (set m ((MySQLConnection alloc) init))
0
+ (set result (m connect))
0
+ (assert_equal 1 result)
0
+ (set result (m query:"create database if not exists NuMySQLTest"))
0
+ (set result (m selectDB:"NuMySQLTest"))
0
+ (set result (m query:<<-END
0
+create table cities (
0
+ id integer PRIMARY KEY AUTO_INCREMENT,
0
+ city text,
0
+ nation text)
0
+END))
0
+ (set result (m insertRowInTable:"cities" withDictionary:(dict city:"San Francisco" nation:"United States")))
0
+ (set result (m insertRowInTable:"cities" withDictionary:(dict city:"Tokyo" nation:"Japan")))
0
+ (set result (m insertRowInTable:"cities" withDictionary:(dict city:"Bangalore" nation:"India")))
0
+ (set result (m insertRowInTable:"cities" withDictionary:(dict city:"Copenhagen" nation:"Denmark")))
0
+ (set result (m query:"select * from cities"))
0
+ (assert_equal 4 (result rowCount))
0
+ (set result ((m queryAsDictionary:"select * from cities" withKey:"nation")))
0
+ (assert_equal "Tokyo" ((result "Japan") "city"))
0
+ (set result (m query:"drop database NuMySQLTest")))
0
      
0
      (- testUpdate is
0
         (set m ((MySQLConnection alloc) init))
0
@@ -102,6 +123,7 @@ END))
0
         (set result (m updateTable:"cities" withDictionary:(dict city:"London" nation:"England") forId:4))
0
         (set result (m queryAsValue:"select * from cities where id = 4"))
0
         (assert_equal "London" (result "city"))
0
- (assert_equal "England" (result "nation"))
0
+ (assert_equal "England" (result "nation"))
0
         (set result (m query:"drop database NuMySQLTest"))))
0
 
0
+

Comments

    No one has commented yet.