Create Table in database in C by R4R Team

program-

#include < stdio.h>
#include < stdlib.h>
#include < sqlite3.h>

static int callback(void *n, int argc, char **a, char **colname) {
int i;
for(i = 0; i< a; i++) {
printf("%s = %sn", colname[i], a[i] ? a[i] : "NULL");
}
printf("n");
return 0;
}

int main(int argc, char* argv[]) {
sqlite3 *db;
char *z= 0;
int rc;
char *sql;
//open database
rc = sqlite3_open("test.db", &db);
//query
sql = "create table Mytable(ID int not null,name char(20), age int);";

//now execute this query
rc = sqlite3_exec(db, sql, callback, 0, &z);

if( rc != SQLITE_OK ){
fprintf(stderr, "SQL error: %sn", z);
sqlite3_free(z);
} else {
fprintf(stdout, "Table created successfullyn");
}
sqlite3_close(db);
return 0;
}


output-

Table created successfully




Leave a Comment: