Class reflection exists in java/C #, but does not exist in C.
In java/C #, tables can be designed as classes, while in C, tables can only be designed as struct.
In java, hibernate is used to operate databases. But how can we design it in C?
Now I have come up with an idea:
Create a struct to indicate the structure of a database table.
Typedef struct User {
Int id;
Char * name;
Char * password;
} User;
Statement for creating a table:
Create table 'user' ('id' integer primary key autoincrement, 'name' varchar (100), 'Password' varchar (100), 'worknumber' varchar (100 ))
Database Operations include select, insert, delete, update, and insert, delete, and update, which allow the database to operate. However, select statements return data.
Therefore, 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;}
Database operations are defined
#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 __)
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 ){
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
Use sqlite3_prepare to find sqlite3_stmt, traverse stmt, retrieve the table name of the current operation, and compare it with the defined struct user. If it is equal to, create a User and send the queried data to the User, 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(User));memset(item,0,sizeof(User));count=sqlite3_column_count(stmt);for(i=0;i
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); printf ("% 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 (table_name, STR (User) = 0) {// you can add it to the list with 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
This is only a query operation for a single table. You can add the count (*) function.
Int count;
Select_table (void **) & count, SELECT _ ("count (*)", User, "1 = 1 "));
For other tables, you only need to create a structure equivalent to the User, then add a select_value_item_user function database value to the function bound to this function, and then select this function in select,
Of course, you can use HashMap under C to map the table name (struct name) with the binding function. This is a little more convenient, but I only have a few tables, so I don't need to do it.