There is a class of reflection in the java/c#, and C does not exist.
In java/c#, tables can be designed into classes, while C can only be designed as structural forms.
There is hibernate in Java to manipulate the database, but how to design it under C?
Now, I came up with an idea for the following using SQLite
First, create a struct that represents the structure of a database table.
typedef struct USER {
int id;
Char *name;
Char *password;
} User;
To create a statement for a table:
CREATE TABLE ' User ' (' id ' INTEGER PRIMARY KEY autoincrement, ' name ' varchar, ' password ' varchar, ' Worknumber ' var char (100))
The operation of the database has SELECT, INSERT, delete, update, and insert,delete,update is to let the database to operate, but the select has returned data.
So, for insert,delete,update I use
int sql_exec (char *format,...) {char sql[1024];va_list Args;char *errmsg=null;int rc;va_start (args, format); vsprintf (Sql,format,args); Va_end (args) ; Rc=sqlite3_exec (g_mdb,sql,null,null,&errmsg); if (RC!=SQLITE_OK) {printf ("%s,%s,%s\n", __function__,errmsg, SQL); Sqlite3_free (errmsg); return sqlite_error;} return SQLITE_OK;}
The operation of the database I define as
#define SELECT_ (_columns,_table,_where) "select" _columns "from" #_table "where" # #_where # define INSERT_INTO_ (_table,_ columns,_values) "INSERT INTO" #_table "(" _columns ") VALUES (" _values ")" #define UPDATE_ (_table,_set,_where) "UPDATE" #_ Table "SET" _set "where" # #_where # define DELETE_FROM (_table,_where) "DELETE from" #_table "where" # #_where
#define INSERT_TABLE (Format,...) Sql_exec (format,__va_args__)
#define UPDATE_TABLE (Format,...) Sql_exec (format,__va_args__)
#define DELETE_TABLE (Format,...) Sql_exec (format,__va_args__)
Final Call (insert):
Insert_table (Insert_into_ (User, "Id,name,password", "%d, '%s ', '%s '"), Us.id,us.name,us.password);
For select, a list of data is returned:
struct select_list {void *value;struct select_list *next;}; typedef void Select_value_free (void *value), struct select_list *select_list_new () {struct select_list *h= (struct SELECT _list*) malloc (sizeof (struct select_list)); memset (h,0,sizeof (struct select_list)); return h;} void Select_list_free (struct select_list *list,select_value_free value_free) {struct select_list *next;if (List==NULL) {return;} if (list->value) {value_free (list->value);} if (list) {next=list->next;free (list);} Select_list_free (Next,value_free);}
Select_list *home;
Select_table ((void**) &home,select_ ("id", User, "Name= '%s ' and password= '%s '"), Us.name,us.password);
if (home!=null) {
for (Next=home;next;next=next->next) {
User *item= ((user*) next->value);
printf ("%d,%s,%s\n", Item->id,item->name,item->password);
}
Select_list_free (Home,user_free);
}
The key is in select_table
Through Sqlite3_prepare find out sqlite3_stmt, Traverse stmt, take out the current operation of the table name, and the definition of the structure of the user, if equal, then create the user, the query data to the user, and then add the user to the linked list value.
void *select_value_item_user (sqlite3_stmt *stmt) {int i;int count; User *item= (user*) malloc (sizeof), memset (item,0,sizeof (user)); Count=sqlite3_column_count (stmt); for (i=0;i <count;i++) {if (0==strcmp (Sqlite3_column_name (stmt,i), "id")) {item->id=sqlite3_column_int (stmt,i);} if (0==strcmp (Sqlite3_column_name (stmt,i), "name")) {char_cpy (&item->name, (const char *) Sqlite3_column_text ( stmt,i));}} return item;}
int select_table (void **result,char *pszformat,...) {SQLITE3_STMT * stmt=null; const char *table_name=null;int count=0;char sql[1024];va_list args; struct select_list *next= Null;struct select_list *home=null;va_start (args, Pszformat); vsprintf (Sql,pszformat,args); Va_end (args);p rintf ("%s\n", SQL), *result=null;if (Sqlite3_prepare (G_mdb,sql,-1, &stmt,null) ==SQLITE_OK) {while (Sqlite3_step (stmt) ==sqlite_row) {/******************************************* *****************************//* *//***************** /table_name=sqlite3_column_table_name (stmt,0); if (Table_ Name) {if (strcmp (User) ==0) {///Add to List Table_name,str next=select_list_new (); Next->value=select_value_item _user (stmt); Next->next=null;if (*result==null) {*result=next;} else {home->next=next;} Home=next;}} else {{int column_count=sqlite3_column_count (stmt); int i=0;for (i=0;i<column_count;i++) {if (Sqlite3_cOlumn_type (stmt,i) ==sqlite_integer) {//printf ("%s,%d\n", Sqlite3_column_name (Stmt,i), Sqlite3_column_int (Stmt,i)) ; *result= (void*) sqlite3_column_int (stmt,i);}}} count++;}} else {count=-1;//save errmsg ...} Sqlite3_finalize (stmt); return count;}
This is only a query operation for a single table, and you can add the count (*) function.
int count;
Select_table ((void**) &count,select_ ("Count (*)", User, "1=1"));
For other tables, you can just create a struct equivalent to user, add a function that select_value_item_user the function database value to this binding function, and select the function in select.
Of course, you can use the C-HashMap to make an image of the table name (struct name) and the binding function, so it's a little easier, but my table is only a few so I don't have to do it.