Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite output rows
#1
Somehow I am doing something wrong in this seemingly simple question.
How do I output rows likle this

c0, c1, c2,...
c0, c1, c2,...
c0, c1, c2,...

c0 = contents colum 0, etc...

I use the QM sqlite functionality and I just want to simply output each row as shown above.

Macro Macro6
Code:
Select All      Help
str dbfile="F:\portableapps\chrome\Data\profile\Default\Cookies" ;; File has NO extension.
str table="cookies" ;; table is also called 'cookies' !
str s
Sqlite db3.Open(dbfile)
ARRAY(str) ar; int r c
db3.Exec(F"SELECT * FROM {table}" ar)
for r 0 ar.len(1) ;;for each row
,for c 0 ar.len(2) ;;for each column?
,,s.from(s ar[r c] ",")
,s.rtrim(",")
,out s
,;Output each row:
,;[c0] , [c1] , ...etc
,;[c0] , [c1] , ...etc
,;[c0] , [c1] , ...etc
,s="" ;; empty 's' for next row...

To be clear: it's just simple row output I want, each row outputs each column.
The above code outputs the contents but not correctly row-wise.
#2
I did not test, but maybe should be ar[c r]
#3
I get invalid index if I reverse 'c' and 'r'
Below I created a test macro for SQLITE. It creates database in windows tempdir, creates table then inserts data.
But still can not get the desired output:
Code:
Select All      Help
-------------------------------------
TOON_ID  |  FIRST_NAME       |  LAST_NAME
-------------------------------------
   1     |      Daffy          |    Duck
   2     |      Elmer          |    Fudd
   3     |      Yosemite        |   Sam
   4     |      Pepe           |    Le Pew
   5     |      Speedy         |    Gonzales
   6     |      Bugs           |    Bunny
   7     |      Sylvester      |    Jr



Macro Macro9
Code:
Select All      Help
str dbfile="$Temp$\toons_test.db" ;; File has NO extension.
if(FileExists(dbfile))del- dbfile ;; If re-running this script, need to delete exesting or else error

str table="toons"
str sql_create_table=
F
;CREATE TABLE {table} (
;;toon_id integer PRIMARY KEY,
;;first_name text NOT NULL,
;;last_name text NOT NULL
;);

str sql_insert=
F
;INSERT INTO {table} VALUES (1, 'Daffy', 'Duck');
;INSERT INTO {table} VALUES (2, 'Elmer', 'Fudd');
;INSERT INTO {table} VALUES (3, 'Yosemite', 'Sam');
;INSERT INTO {table} VALUES (4, 'Pepe', 'Le Pew');
;INSERT INTO {table} VALUES (5, 'Speedy', 'Gonzales');
;INSERT INTO {table} VALUES (6, 'Bugs', 'Bunny');
;INSERT INTO {table} VALUES (7, 'Sylvester', 'Jr');

Sqlite db3.Open(dbfile) ;; Connect or Create database
db3.Exec(F"{sql_create_table}") ;; Create table
db3.Exec(F"{sql_insert}") ;; Insert data


ARRAY(str) ar; int r c
db3.Exec(F"SELECT * FROM {table}" ar)
for r 0 ar.len ;;for each row
,out ar[0 r]
,out ar[1 r]
,out ar[2 r]
,out "---"
#4
Macro Macro2835
Code:
Select All      Help
str s
ARRAY(str) ar; int r c
db3.Exec(F"SELECT * FROM {table}" ar)
for r 0 ar.len ;;for each row
,s.formata("  %-4s |  %-15s |  %s[]" ar[0 r] ar[1 r] ar[2 r])
out s
#5
Ok thank you!


Forum Jump:


Users browsing this thread: 1 Guest(s)