Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. //csv to sqlite by lovecraft#4690
  2. #ifndef _GNU_SOURCE
  3. #define _GNU_SOURCE 1
  4. #endif
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <sqlite3.h>
  11.  
  12. #define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))
  13.  
  14. int ti;
  15. int i;
  16. int x;
  17. int accumulator;
  18. int total_accumulated;
  19. int n;
  20. int vi; //values inserted
  21. int tp; //total processed per file
  22. int remaining;
  23. char *wallet[1000000];
  24. char *privkey[1000000];
  25. char *db_name[5000];
  26.  
  27. int processed;
  28. const int db_maxsize = 1000000;
  29.  
  30. const char* getfield(char* line, int num)
  31. {
  32.     const char* tok;
  33.     for (tok = strtok(line, ",");
  34.             tok && *tok;
  35.             tok = strtok(NULL, ",\n"))
  36.            
  37.     {
  38.         if (!--num)
  39.             return tok;
  40.     }
  41.     return NULL;
  42. }
  43.  
  44. int main()
  45. {
  46.     system("clear");
  47.     int rc;
  48.     char *zErrMsg  = NULL;
  49.     char *sql      = NULL;
  50.     sqlite3 *db;
  51.     FILE *stream = fopen("output.csv", "r");
  52.     char currentline[1000];
  53.     char *query = NULL;
  54.     char *pk = NULL;   
  55.     assert(stream != NULL);
  56.     sqlite3_stmt *stmt;
  57.    
  58.  
  59.         //(void)fclose(stream);
  60.     char line[1024];
  61.  
  62.  
  63.  
  64.  
  65.     /* Open database */
  66.     rc = sqlite3_open_v2("test1.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_CONFIG_MULTITHREAD, NULL);
  67.     if (rc) {
  68.         fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  69.        
  70.        
  71.         } else {
  72.             gotoxy(9, 40);
  73.             fprintf(stdout, " ____________________________ \n");
  74.             gotoxy(10, 40);
  75.             fprintf(stdout, "*                            *\n");
  76.             gotoxy(11, 40);
  77.             fprintf(stdout, "*Opened database successfully*\n");
  78.             gotoxy(12, 40);
  79.             fprintf(stdout, "*____________________________*\n");
  80.         }
  81.                
  82.        
  83.         sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS BITCOIN (ADDRESS TEXT, PRIVKEY TEXT)", NULL, NULL, NULL);
  84.         /* Insert generated address into database */
  85.         sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION;", NULL, NULL, NULL);
  86.         while (fgets(currentline, sizeof(currentline), stream) != NULL) {
  87.             x++;
  88.             i++;
  89.             char* tmp = strdup(currentline);
  90.             gotoxy(14, 0);
  91.             asprintf(&pk,"%s",getfield(tmp,2));
  92.             printf("Importing: \nWallet:%s \nPrivate key: %s ", getfield(tmp, 1),pk);
  93.  
  94.             asprintf(&query, "INSERT INTO BITCOIN (ADDRESS, PRIVKEY) VALUES('%s','%s')",getfield(tmp, 1), pk); //whatevs primal
  95.             gotoxy(20, 0);
  96.                 printf("%s",query);
  97.             sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); // <---I really want to use this (I finally am!)
  98.             /* Execute Insert */
  99.         rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg);
  100.         free(tmp);
  101.         if (rc != SQLITE_OK) {
  102.             fprintf(stderr, "INSERT SQL error: %s\n", zErrMsg);
  103.             sqlite3_free(zErrMsg);
  104.         } else {
  105.            
  106.             gotoxy(17, 0);
  107.             fprintf(stdout, "\rINSERTed record %d successfully", x);
  108.         }
  109.         }//end for loop
  110.         fprintf(stdout, "\n");
  111.         sqlite3_finalize(stmt);
  112.  
  113.         sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, NULL, NULL);
  114.         sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);
  115.         free(query);
  116.         sqlite3_close(db);
  117.         fclose(stream);
  118.        
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement