Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //csv to sqlite by lovecraft#4690
- #ifndef _GNU_SOURCE
- #define _GNU_SOURCE 1
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sqlite3.h>
- #define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))
- int ti;
- int i;
- int x;
- int accumulator;
- int total_accumulated;
- int n;
- int vi; //values inserted
- int tp; //total processed per file
- int remaining;
- char *wallet[1000000];
- char *privkey[1000000];
- char *db_name[5000];
- int processed;
- const int db_maxsize = 1000000;
- const char* getfield(char* line, int num)
- {
- const char* tok;
- for (tok = strtok(line, ",");
- tok && *tok;
- tok = strtok(NULL, ",\n"))
- {
- if (!--num)
- return tok;
- }
- return NULL;
- }
- int main()
- {
- system("clear");
- int rc;
- char *zErrMsg = NULL;
- char *sql = NULL;
- sqlite3 *db;
- FILE *stream = fopen("output.csv", "r");
- char currentline[1000];
- char *query = NULL;
- char *pk = NULL;
- assert(stream != NULL);
- sqlite3_stmt *stmt;
- //(void)fclose(stream);
- char line[1024];
- /* Open database */
- rc = sqlite3_open_v2("test1.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_CONFIG_MULTITHREAD, NULL);
- if (rc) {
- fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
- } else {
- gotoxy(9, 40);
- fprintf(stdout, " ____________________________ \n");
- gotoxy(10, 40);
- fprintf(stdout, "* *\n");
- gotoxy(11, 40);
- fprintf(stdout, "*Opened database successfully*\n");
- gotoxy(12, 40);
- fprintf(stdout, "*____________________________*\n");
- }
- sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS BITCOIN (ADDRESS TEXT, PRIVKEY TEXT)", NULL, NULL, NULL);
- /* Insert generated address into database */
- sqlite3_exec(db, "BEGIN EXCLUSIVE TRANSACTION;", NULL, NULL, NULL);
- while (fgets(currentline, sizeof(currentline), stream) != NULL) {
- x++;
- i++;
- char* tmp = strdup(currentline);
- gotoxy(14, 0);
- asprintf(&pk,"%s",getfield(tmp,2));
- printf("Importing: \nWallet:%s \nPrivate key: %s ", getfield(tmp, 1),pk);
- asprintf(&query, "INSERT INTO BITCOIN (ADDRESS, PRIVKEY) VALUES('%s','%s')",getfield(tmp, 1), pk); //whatevs primal
- gotoxy(20, 0);
- printf("%s",query);
- sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); // <---I really want to use this (I finally am!)
- /* Execute Insert */
- rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg);
- free(tmp);
- if (rc != SQLITE_OK) {
- fprintf(stderr, "INSERT SQL error: %s\n", zErrMsg);
- sqlite3_free(zErrMsg);
- } else {
- gotoxy(17, 0);
- fprintf(stdout, "\rINSERTed record %d successfully", x);
- }
- }//end for loop
- fprintf(stdout, "\n");
- sqlite3_finalize(stmt);
- sqlite3_exec(db, "COMMIT TRANSACTION;", NULL, NULL, NULL);
- sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, NULL);
- free(query);
- sqlite3_close(db);
- fclose(stream);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement