Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. INSERT INTO "MyTable" VALUES ('A', 'B');
  2.  
  3. INSERT INTO "MyTable" (Column1, Column2) VALUES ('A', 'B');
  4.  
  5. sqlite> .output test.data
  6. sqlite> pragma table_info(test);
  7. sqlite> .dump test
  8. sqlite> .quit
  9.  
  10. $ cat test.data
  11. 0|test_id|int|0||1
  12. 1|test_name|varchar(35)|0||0
  13. PRAGMA foreign_keys=OFF;
  14. BEGIN TRANSACTION;
  15. CREATE TABLE test (test_id int primary key, test_name varchar(35));
  16. INSERT INTO "test" VALUES(1,'Wibble');
  17. INSERT INTO "test" VALUES(2,'Wobble');
  18. INSERT INTO "test" VALUES(3,'Pernicious');
  19. COMMIT;
  20.  
  21. /|/ {
  22. split($0, col_name, "|");
  23. column_names[++n] = col_name[2];
  24. }
  25. /INSERT INTO "[A-Za-z].*"/ {
  26. insert_part = match($0, /INSERT INTO "[A-Za-z].*"/);
  27. printf("%s ", substr($0, RSTART, RLENGTH));
  28.  
  29. printf("(");
  30. for (i = 1; i <= n; i++) {
  31. if (i == 1) {
  32. printf("%s", column_names[i]);
  33. }
  34. else {
  35. printf(", %s", column_names[i]);
  36. }
  37. }
  38. printf(") ");
  39.  
  40. values_part = substr($0, RLENGTH+1, length($0) - RSTART);
  41. printf("%sn", values_part);
  42.  
  43.  
  44. }
  45.  
  46. $ awk -f dump_with_col_names.awk test.data
  47. INSERT INTO "test" (test_id, test_name) VALUES(1,'Wibble');
  48. INSERT INTO "test" (test_id, test_name) VALUES(2,'Wobble');
  49. INSERT INTO "test" (test_id, test_name) VALUES(3,'Pernicious');
  50.  
  51. PRAGMA foreign_keys=OFF;
  52. BEGIN TRANSACTION;
  53. CREATE TABLE test (test_id int primary key, test_name varchar(35));
  54. INSERT INTO "test" VALUES(1,'Wibble');
  55. INSERT INTO "test" VALUES(2,'Wobble');
  56. INSERT INTO "test" VALUES(3,'Pernicious');
  57. COMMIT;
  58.  
  59. /CREATE TABLE/ {
  60. # Extract the part between parens. This part contains the
  61. # column definitions.
  62. first_col = match($0, /(.*)/ );
  63. if (first_col) {
  64. num_columns = split(substr($0, RSTART + 1, RLENGTH), a, ",");
  65. for (i = 1; i <= num_columns; i++) {
  66. sub(/^ /, "", a[i]);
  67. split(a[i], names, " ");
  68. column_names[i] = names[1];
  69. }
  70. }
  71. }
  72. /INSERT INTO "[A-Za-z].*"/ {
  73. insert_part = match($0, /INSERT INTO "[A-Za-z].*"/);
  74. printf("%s ", substr($0, RSTART, RLENGTH));
  75.  
  76. printf("(");
  77. for (j = 1; j <= num_columns; j++) {
  78. if (j == 1) {
  79. printf("%s", column_names[j]);
  80. }
  81. else {
  82. printf(", %s", column_names[j]);
  83. }
  84. }
  85. printf(") ");
  86.  
  87. values_part = substr($0, RLENGTH+1, length($0) - RSTART);
  88. printf("%sn", values_part);
  89.  
  90. }
  91.  
  92. INSERT INTO "test" (test_id, test_name) VALUES(1,'Wibble');
  93. INSERT INTO "test" (test_id, test_name) VALUES(2,'Wobble');
  94. INSERT INTO "test" (test_id, test_name) VALUES(3,'Pernicious');
  95.  
  96. #!/usr/bin/perl
  97.  
  98. use strict;
  99. use warnings;
  100.  
  101. my @column_names;
  102. my $col_reset = 1;
  103.  
  104. while(<>)
  105. {
  106. if (/^d+|/) {
  107. if ($col_reset)
  108. {
  109. @column_names = ();
  110. $col_reset = 0;
  111. }
  112. my @col_info = split(/|/);
  113. push @column_names, $col_info[1];
  114. }
  115.  
  116. if(/INSERT INTO/) {
  117. m/(INSERT INTO "?[A-Za-z_]+"?) (.*)/ or die $_;
  118. my $insert_part = $1;
  119. my $values_part = $2;
  120. print $insert_part." (".join(",", @column_names).") ".$values_part."n";
  121. $col_reset = 1;
  122. }
  123. }
  124.  
  125. grep 'CREATE TABLE' /tmp/school.sql.final
  126. | awk '{ print $3 }'
  127. | while read table; do
  128. echo -e "pragma table_info($table);n.dump $table"
  129. done | sqlite3 school.db
  130. > /tmp/school.sql.final-with-table-info
  131.  
  132. BEGIN {
  133. state = "default" # Used to know if we are in the middle of a table declaration
  134. print_definitions = 1 # Wether to print CREATE statements or not
  135. }
  136.  
  137. state == "default" && match($0, /^CREATE TABLE ([A-Za-z0-9_]+)/, a) {
  138. tablename = a[1]
  139. state = "definition"
  140. if (print_definitions)
  141. print
  142. next
  143. }
  144.  
  145. state == "definition" && /^);$/ {
  146. state = "default"
  147. if (print_definitions)
  148. print
  149. next
  150. }
  151.  
  152. state == "definition" && ! ( /PRIMARY/ || /UNIQUE/ || /CHECK/ || /FOREIGN/) {
  153. if (length(columnlist [tablename]))
  154. columnlist[tablename] = columnlist[tablename] ", "
  155. columnlist[tablename] = columnlist[tablename] $1
  156. if (print_definitions)
  157. print
  158. next
  159. }
  160.  
  161. state == "default" && match($0, /^(INSERT INTO ")([A-Za-z0-9_]+)"(.*)$/, a) {
  162. print a[1] a[2] "" (" columnlist[a[2]] ")" a[3]
  163. }
  164.  
  165. CREATE TABLE "strom" (
  166. "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  167. "otec" integer NOT NULL,
  168. "nazev" text NOT NULL,
  169. "ikona" text NULL,
  170. "barva" text NULL
  171. );
  172.  
  173. CREATE TABLE "changes" (
  174. "version" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  175. "last_change" text NOT NULL DEFAULT (datetime('now','localtime')),
  176. `ref` text NOT NULL,
  177. "ref_id" text NULL,
  178. "action" text NOT NULL
  179. , "data" text NOT NULL DEFAULT '');
  180.  
  181. #!/usr/bin/gawk -f
  182.  
  183. # input is sqlite3 dump, tested with sqlite3 version 3.8.7.1
  184. # output are INSERT statements including column names
  185. # i.e. not e.g.
  186. # INSERT INTO "changes" VALUES(1,'2016-07-19 17:46:12','cenik','10','UPDATE','');
  187. # like in standard dump
  188. # but
  189. # INSERT INTO "changes" ("version", "last_change", "ref", "ref_id", "action", "data") VALUES(1,'2016-07-19 17:46:12','cenik','10','UPDATE','');
  190. # BEGIN TRANSACTION and COMMIT are included in output
  191.  
  192. BEGIN {
  193. state = "default" # default/definition/insert let us know wether we are in CREATE or INSERT statement
  194. print_definitions = 0 # wether to print CREATE statements or not
  195. }
  196.  
  197. state == "default" && match($0, /^CREATE TABLE "([A-Za-z0-9_]+)" *($/, a) {
  198. tablename = a[1]
  199. state = "definition"
  200. if (print_definitions)
  201. print
  202. next
  203. }
  204.  
  205. state == "definition" && /^ *); *$/ {
  206. state = "default"
  207. if (print_definitions)
  208. print
  209. next
  210. }
  211.  
  212. state == "definition" && ! ( /^[ ]{1,2}PRIMARY/ || /UNIQUE/ || /CHECK/ || /^[ ]{1,2}FOREIGN KEY.*REFERENCES/) {
  213. if (length(columnlist [tablename]))
  214. columnlist[tablename] = columnlist[tablename] ", "
  215. if (match($0, /(".*")/, b))
  216. columnlist[tablename] = columnlist[tablename] b[1]
  217. if (match($0, /`(.*)`/, c))
  218. columnlist[tablename] = columnlist[tablename] """c[1]"""
  219. if (print_definitions)
  220. print
  221. }
  222.  
  223. state == "definition" && /^.*); *$/ {
  224. state = "default"
  225. next
  226. }
  227.  
  228. state == "default" && match($0, /^(INSERT INTO ")([A-Za-z0-9_]+)"(.*)/, a) {
  229. print a[1] a[2] "" (" columnlist[a[2]] ")" a[3]
  230. state = "insert"
  231. if (/^.*); *$/)
  232. state = "default"
  233. }
  234.  
  235. state == "insert" && ! /^INSERT INTO/{
  236. print
  237. }
  238.  
  239. state == "insert" && /^.*); *$/ {
  240. state = "default"
  241. next
  242. }
  243.  
  244. state == "default" && (/^ *BEGIN TRANSACTION;/ || /^ *COMMIT;/) {
  245. print
  246. }
  247.  
  248. sqlite> .headers on
  249. sqlite> .mode insert MyTable
  250. sqlite> .output MyTable_data.sql
  251. sqlite> select * from MyTable;
  252. sqlite> .quit
  253.  
  254. sudo apt install libsqlite3-mod-impexp
  255.  
  256. .load libsqlite3_mod_impexp
  257.  
  258. select export_sql('dump.sql','1');
  259.  
  260. import sqlite3
  261. infile="your_file.sqlite3"
  262. table="your_table"
  263.  
  264. conn = sqlite3.connect(infile)
  265. conn.row_factory = sqlite3.Row
  266.  
  267. c = conn.cursor()
  268. res = c.execute("SELECT * FROM " + table)
  269. curr_row = -1
  270.  
  271. for row in res:
  272. curr_row += 1
  273. if curr_row == 0:
  274. col_names = sorted(row.keys())
  275. s = "INSERT INTO " + table + " ("
  276. for col_name in col_names:
  277. s+=col_name + ","
  278. prefix = s[:-1] + ") VALUES ("
  279.  
  280. s = ""
  281. for col_name in col_names:
  282. col_val = row[col_name]
  283. if isinstance(col_val,int) or isinstance(col_val,float):
  284. s+= str(row[col_name]) +","
  285. else:
  286. s+= "'" + str(row[col_name]) +"',"
  287. print prefix,s[:-1],");"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement