使用sqlite3创建数据库

前端之家收集整理的这篇文章主要介绍了使用sqlite3创建数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

创建数据库

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

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

int main(int argc,char* argv[])
{
   sqlite3 *db;
   char *zErrMsg = 0;
   int  rc;
   char *sql;

   /* Open database */
   rc = sqlite3_open("test.db",&db);
   if( rc ){
      fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
      exit(0);
   }else{
      fprintf(stdout,"Opened database successfully\n");
   }

   /* Create sql statement */
   sql = "CREATE TABLE COMPANY("  \
         "ID INT PRIMARY KEY NOT NULL," \
         "NAME TEXT NOT NULL," \
         "AGE INT NOT NULL," \
         "ADDRESS CHAR(50)," \
         "SALARY REAL );";

   /* Execute sql statement */
   rc = sqlite3_exec(db,sql,callback,0,&zErrMsg);
   if( rc != sqlITE_OK ){
   fprintf(stderr,"sql error: %s\n",zErrMsg);
      sqlite3_free(zErrMsg);
   }else{
      fprintf(stdout,"Table created successfully\n");
   }
   sqlite3_close(db);
   return 0;
}

编译

gcc -o main test.c -l sqlite3
// * 链接 sqlite3 库

输出

参考

创建数据库: www.runoob.com/sqlite/sqlite-c-cpp.html Makefile: blog.csdn.net/haoel/article/details/2887

原文链接:https://www.f2er.com/sqlite/198506.html

猜你在找的Sqlite相关文章