Friday, April 1, 2022

TCC, SQLITE, UTF-8 super working (command line tool creation) * pure C is awesome

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

 // Compile instructions:
// tcc ctext.c sqlite3.def

// If you use ulaz.txt file saved as utf-8 text file,
// than first line of text must be empty
// just put all select, insert, update and other sqlite commands in ulaz.txt
//line by line
//callback can be modified to write data formated to HTML TABLE, than open it

  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[])
int main()
{

char const* const fileName = "ulaz.txt";

FILE* file = fopen(fileName, "r");

if(!file){
printf("\n Unable to open : %s ", fileName);
return -1;
}

    char line[500];
    const char algv1[8] = "dora.db";

    //algv1 = "dora.db";

    char str[50];
    FILE *fp;
    fp = fopen("izlaz2.txt", "w");

    if(fp == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

/*  
    if( argc!=3 ){
      fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
      return(1);
    }
*/

    rc = sqlite3_open("dora.db", &db);
    if( rc ){
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      sqlite3_close(db);
      return(1);
    }

/*
    rc = sqlite3_exec(db, "select * from dora where a4 like '%Сремски%'", callback, 0, &zErrMsg);
    if( rc!=SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
    }

*/

//    sqlite3_close(db);

int i=0;
 
while (fgets(line, sizeof(line), file)) {
        printf("%s", line);
//        fputs(str, fp);
        fputs(line, fp);

if (i>0) {
    rc = sqlite3_exec(db, line, callback, 0, &zErrMsg);
    if( rc!=SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
    }
}
    i = i + 1;

    }

    fclose(fp);
    fclose(file);
        sqlite3_close(db);

    return 0;

} 

OR YOU CAN TRY THIS IN GOLANG TOO:


But in GOLANG, you must to install GCC first and:

https://github.com/mattn/go-sqlite3

too many hussle for the same result.

On Windows GCC best installation is MinGW way.

Hundreds and hundreds megabytes for simple peace of code.

On the other hand in TCC you need to install only 1,2 MB and we are in business.

TCC need only dll and def files from main SQLITE web site.

No comments:

Post a Comment

Коментар: