· 8 years ago · Jan 11, 2018, 08:04 PM
1#include <stdlib.h>
2#include <stdio.h>
3#include <unistd.h>
4#include "sqlite3.h"
5// name: sqlitetest
6// Compiled with:
7// /opt/gcc-6/bin/gcc -m64 -lsqlite3 test.c -o sqlitetest
8
9int main( int argc, char **argv )
10{
11 char *file = "/tmp/test.db"; /* default to db in /tmp */
12 sqlite3 *db = NULL;
13 sqlite3_stmt *stmt = NULL;
14 int rc = 0;
15 long counter = 0;
16 int open_flags = (
17 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
18 SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_URI
19 );
20
21 char *tbl_create = "CREATE TABLE IF NOT EXISTS testing ( "
22 "a TEXT UNIQUE, "
23 "b TEXT PRIMARY KEY NOT NULL, "
24 "c TEXT NOT NULL REFERENCES pools (pool_guid) ON DELETE CASCADE,"
25 "d TEXT);" ;
26
27 if ( argc > 1 )
28 file = argv[1];
29
30 size_t need = snprintf(NULL, 0, "file:%s", file);
31 char *uri = malloc(need+1);
32 sprintf(uri, "file:%s", file);
33
34 while ( 1 ) {
35 sqlite3_initialize( );
36 rc = sqlite3_open_v2( uri, &db, open_flags, NULL );
37 if ( rc != SQLITE_OK) {
38 printf("sqlite failed to do sqlite3_open_v2 with rc: %d\n", rc);
39 sqlite3_close( db );
40 exit( -1 );
41 }
42
43 rc = sqlite3_prepare_v2( db, tbl_create, -1, &stmt, NULL );
44
45 if ( rc != SQLITE_OK) {
46 printf("sqlite failed to do sqlite3_prepare_v2 with rc: %d\n", rc);
47 exit( -1 );
48 }
49
50 rc = sqlite3_step( stmt );
51 if ( rc != SQLITE_DONE ) {
52 printf("sqlite failed to do sqlite3_step with rc: %d\n", rc);
53 exit ( -1 );
54 }
55
56 sqlite3_finalize( stmt );
57 sqlite3_close( db );
58 sqlite3_shutdown( );
59
60 rc = unlink(file);
61 if ( rc != 0 ) {
62 printf("sqlite failed to do sqlite3_prepare_v2 with rc: %d\n", rc);
63 exit(rc);
64 }
65 db = NULL;
66 stmt = NULL;
67
68 counter++;
69 if ((counter % 1000) == 0) {
70 printf("loops: %ld\n", counter);
71 }
72 }
73
74 free(uri);
75 return 0;
76}