-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3.cpp
80 lines (65 loc) · 1.51 KB
/
sqlite3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* =====================================================================================
*
* Filename: sqlite3.cpp
*
* Description:
*
* Version: 1.0
* Created: 09/13/2017 03:07:51 PM
* Last Modified: 09/13/2017 03:07:51 PM
* Revision: none
* Compiler: gcc
*
* Author: zt (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static int callback ( void* NotUsed, int argc, char** argv, char** azColName )
{
int i = 0;
for ( i = 0; i < argc; ++i )
{
printf ( "%s=%s\n", azColName[i], argv[i] ? argv[i] : "NULL" );
}
printf ( "\n" );
return 0;
}
int main ( int argc, char* argv[] )
{
sqlite3* db = NULL;
char* zErrMsg = 0;
int rc;
rc = sqlite3_open ( "test.db", &db );
if ( rc )
{
fprintf ( stderr, "cant't open database: %s\n", sqlite3_errmsg ( db ) );
exit ( 0 );
}
else
{
fprintf ( stdout, "open ok\n" );
}
char sql[] = "CREATE TABLE COMPANY("\
"ID INT PRIMARY KEY NOT NULL,"\
"NAME TEXT NOT NULL,"\
"AGE INT NOT NULL,"\
"ADDRESS CHAR(50),"\
"SALARY REAL);";
rc = sqlite3_exec ( db, sql, callback, 0, &zErrMsg );
if ( rc != SQLITE_OK )
{
fprintf ( stderr, "SQL error:%s\n", zErrMsg );
sqlite3_free ( zErrMsg );
}
else
{
fprintf ( stdout, "ok!!!\n" );
}
sqlite3_close ( db );
return 0;
}