Source code:
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
static char *createsql = "CREATE TABLE Employee(" "ID INTEGER PRIMARY KEY," "Name VARCHAR(10)," "BadgeID VARCHAR(10));";
static char *insertsql = "INSERT INTO Employee VALUES(NULL, 'Danny', '12345');";
static char *querysql = "SELECT * FROM Employee;";
int main(void)
{
int i,j;
int rows, cols;
sqlite3 *db;
char *errMsg = NULL;
char **result;
/* Open database file */
if (sqlite3_open("my_example.db3", &db))
{
return 0;
}
/* Build Table */
sqlite3_exec(db, createsql, 0, 0, &errMsg);
/* Add a new record */
sqlite3_exec(db, insertsql, 0, 0, &errMsg);
/* Get all records in database */
sqlite3_get_table(db,querysql,&result, &rows, &cols, &errMsg);
/* List all the data */
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
printf("%s\t", result[i*cols+j]);
}
printf("\n");
}
/* Free table */
sqlite3_free_table(result);
/* Close database */
sqlite3_close(db);
}
My output:
1st time:
hong@ubuntu:~/test$ gcc -o qqq qqq.c -lsqlite3
hong@ubuntu:~/test$ ./qqq
ID Name BadgeID
hong@ubuntu:~/test$
2nd time:
hong@ubuntu:~/test$ ./qqq
ID Name BadgeID
1 Danny 12345
hong@ubuntu:~/test$
How to solve the 2nd time output show on the 1st time?Suppose the output should show after ./qqq at the 1st time.
Bookmarks