· 7 years ago · Sep 13, 2018, 02:32 AM
1Integer not stored in database (sqlite3)
2float lastClicked = 0.0;
3
4- (void)viewDidLoad
5{
6[super viewDidLoad];
7
8NSString *contentDirectory;
9NSArray *directoryPath;
10const char *dbpath = [databasePath UTF8String];
11
12directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
13contentDirectory = [directoryPath objectAtIndex:0];
14
15databasePath = [[NSString alloc] initWithString: [contentDirectory stringByAppendingPathComponent: @"MCFRatingDatabase.db"]];
16
17NSFileManager *filemanager = [NSFileManager defaultManager];
18if ([filemanager fileExistsAtPath:databasePath] == NO){
19
20
21 if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK)
22 {
23 char *errorMessage;
24 const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGERATING (CONTENTNAME STRING, RATING FLOAT)";
25 if (sqlite3_exec(connectDB, sql_stmt, NULL, NULL, &errorMessage) != SQLITE_OK)
26 {
27 NSLog(@"FAILED TO CREAT TABLE");
28 }
29 sqlite3_close(connectDB);
30
31 } else {
32 NSLog(@"FAILED TO OPEN/CREATE DATABASE");
33 }
34}
35[filemanager release];
36
37sqlite3_stmt *statement;
38
39if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK)
40{
41 NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM IMAGERATING WHERE CONTENTNAME = '%@'", titleString];
42 const char *query_stmt = [querySQL UTF8String];
43
44 if (sqlite3_prepare_v2(connectDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
45 {
46 if (sqlite3_step(statement) == SQLITE_ROW)
47 {
48 display = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
49 rate = sqlite3_column_double(statement, 2);
50 NSLog(@"contents are name = %@ and rating = %f", display, rate);
51 }else{
52
53 }
54 sqlite3_finalize(statement);
55 }
56 sqlite3_close(connectDB);
57}
58}
59
60
61-(IBAction)rateSubmitClick:(id)sender{
62
63sqlite3_stmt *statement;
64NSString *insertSQL;
65
66const char *dbpath = [databasePath UTF8String];
67
68if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK){
69
70 if(isHalfClicked){
71 rating = lastClicked + 0.5;
72 insertSQL = [NSString stringWithFormat: @"INSERT INTO IMAGERATING (CONTENTNAME, RATING) VALUES ("%@", %f)", titleString, rating];
73 }else{
74
75 rating = lastClicked;
76 insertSQL = [NSString stringWithFormat: @"INSERT INTO IMAGERATING (CONTENTNAME, RATING) VALUES ("%@", %f)", titleString, rating];
77 }
78
79 const char *insert_stmt = [insertSQL UTF8String];
80
81 sqlite3_prepare_v2(connectDB, insert_stmt, -1, &statement, NULL);
82 if (sqlite3_step(statement) == SQLITE_DONE){
83
84 NSLog(@"Added records are = %@ %f", titleString, rating);
85 isRated=YES;
86 }else{
87 NSLog(@"FAILED TO ADD RECORD");
88 }
89 sqlite3_finalize(statement);
90 sqlite3_close(connectDB);
91}
92}
93
94INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES ("mycontentname", 4.5);
95
96if(isHalfClicked)
97 {
98 rating=lastClicked+0.5;
99 insertSQL = [NSString stringWithFormat: @"INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES ("%@", %f)",titleString,rating];
100 }
101 else {
102
103 insertSQL = [NSString stringWithFormat: @"INSERT INTO USERRATING (CONTENTNAME, RATING) VALUES ("%@", %d)",titleString,lastClicked ];
104 }