-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_data.c
More file actions
69 lines (53 loc) · 1.98 KB
/
view_data.c
File metadata and controls
69 lines (53 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include "sqlite/sqlite3.h"
int main(int argc, char **argv) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open(argv[1], &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
sqlite3_stmt *stmt;
const char *sql = "SELECT name, sql FROM sqlite_master WHERE type='table'";
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
const char *table_name = (const char *) sqlite3_column_text(stmt, 0);
const char *table_schema = (const char *) sqlite3_column_text(stmt, 1);
printf("Table: %s\n", table_name);
printf("Schema: %s\n", table_schema);
const char *select_sql = sqlite3_mprintf("SELECT * FROM %Q", table_name);
sqlite3_stmt *select_stmt;
rc = sqlite3_prepare_v2(db, select_sql, -1, &select_stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(stmt);
sqlite3_close(db);
return 1;
}
int num_cols = sqlite3_column_count(select_stmt);
for (int i = 0; i < num_cols; i++) {
const char *col_name = sqlite3_column_name(select_stmt, i);
printf("%s\t", col_name);
}
printf("\n");
while (sqlite3_step(select_stmt) == SQLITE_ROW) {
for (int i = 0; i < num_cols; i++) {
const char *col_value = (const char *) sqlite3_column_text(select_stmt, i);
printf("%s\t", col_value);
}
printf("\n");
}
sqlite3_finalize(select_stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
printf("Conversion successful\n");
return 0;
}