C-Language Universal Database Interface (SQLite digestion, analog C#,java reflection)

Source: Internet
Author: User
Tags sqlite strcmp

There is a class of reflection in the java/c#, and C does not exist.

The table can be designed into classes in java/c#. and C can only be designed to form a structural body.

There are hibernate in Java to manipulate the database, but how to design under C?

Now, I came up with an idea to use SQLite below

Create a struct first. 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__)

Finally called (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

Find out sqlite3_stmt through Sqlite3_prepare, Traverse stmt, and take out the table name of the current operation. Compared to the defined struct body user, the assumption is equal to. The user is created, the query data is given to the user, and the user is added to the 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 (Table_name,str (User)) ==0) {//Join to list to be able to 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 can increase the count (*) function.

int count;

Select_table ((void**) &count,select_ ("Count (*)", User, "1=1"));

For other tables, you can just create a struct that is equivalent to user. Add a function that select_value_item_user the function database value to this binding function. Select the function in the select again.

Of course, the ability to use the C HashMap to make the table name (struct name) and the binding function an image, so convenient, but my table only a few so do not have to do.

C-Language Universal Database Interface (SQLite digestion, analog C#,java reflection)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.