Here's what I am trying to do:
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute(CREATE table Movies(index integer, mvnm text, year integer, genere text))
SyntaxError: invalid syntax
It highlights table in red color.
There are few problems:
The SQL should be provided in quotes because the execute() method accepts a
string argument.
index is a reserved keyword in SQL, so you cannot use
that as the name of your column.
If you want to run this script
repeatedly, you should add IF NOT EXISTS clause. Otherwise
consequent runs will fail due to "table already exists"
So, after these changes the statement looks like below:
c.execute("CREATE table IF NOT EXISTS Movies(myindex integer, mvnm text, year integer, genere text)")
You can then verify the table creation by logging in to sqllite:
$> ~ $ sqlite3 example.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .tables
Movies
Related
This seems odd to me and violates my previous experience with SQLite. I have a trivial database with one table and 3 columns. When I query the first column I get 'Error: no such column: Name'. The other columns work fine. Log below. There must be something ridiculously basic that I am missing. My sleazy work-around is to add a dummy first column, at which point querying on 'Name' works fine, but that is pretty unsatisfactory. Running on a Mac mini (M1, 2020), macOS Monterey Version 12.5.1.
bash-3.2$ head Names/nameTest1.csv
Name,Gender,Count
James,M,5304407
John,M,5260831
Robert,M,4970386
...
bash-3.2$ sqlite3 test1.db
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.
sqlite> .mode csv
sqlite> .separator ','
sqlite> .import Names/nameTest1.csv testtable1
sqlite> .schema
.schema
CREATE TABLE testtable1(
"Name" TEXT,
"Gender" TEXT,
"Count" TEXT
);
sqlite> select * from testtable1 where Count='5304407';
James,M,5304407
sqlite> select * from testtable1 where Name='James';
Error: no such column: Name
sqlite> .quit
.quit
Your CSV file has the BOM field. It does not correspond to a printable character, so you do not see it, but SQLite fails to strip it and includes it as the the prefix of your Name field. This is a known issue, but recent versions of SQLite should handle it properly. You might be using an older version. Either upgrade your SQLite or strip the BOM code before importing.
I have a database file with log data. The database contains a table LOG and the table contains a column MSG. There are 30 rows in the table, where the MSG column contains the string "down" at the end of the line:
$ sqlite3 log.db "select msg from log" | grep down$ | wc -l
30
But when I try to find them with LIKE, I get no match:
$ sqlite3 log.db "select msg from log where msg like '%down'" | grep down$ | wc -l
0
What could be the reason for this?
Update: MCVE
CREATE TABLE LOG (MSG VARCHAR(6291456) NOT NULL);
INSERT INTO LOG VALUES (X'666163696c6974793d6461656d6f6e3b636f6d706f6e656e743d6e616d65643b746578743d7368757474696e6720646f776e');
SELECT MSG FROM LOG; -- returns the row
SELECT MSG FROM LOG WHERE MSG LIKE '%down'; -- returns nothing
SELECT MSG FROM LOG WHERE MSG LIKE '%down%'; -- returns nothing
SELECT MSG FROM LOG WHERE CAST(MSG AS VARCHAR) LIKE '%down'; -- returns the row
I have no idea why a cast from VARCHAR to VARCHAR makes a difference.
Update: Another MCVE
CREATE TABLE LOG (MSG VARCHAR(6291456) NOT NULL);
INSERT INTO LOG VALUES (X'666163696c6974793d6461656d6f6e3b636f6d706f6e656e743d6e616d65643b746578743d7368757474696e6720646f776e');
INSERT INTO LOG VALUES ('facility=daemon;component=named;text=shutting down');
SELECT ROWID,MSG FROM LOG; -- returns both rows
SELECT ROWID,MSG FROM LOG WHERE MSG LIKE '%down'; -- returns just the second
SELECT ROWID,MSG FROM LOG WHERE MSG LIKE '%down%'; -- returns just the second
SELECT ROWID,MSG FROM LOG WHERE CAST(MSG AS VARCHAR) LIKE '%down'; -- returns both rows
SELECT HEX(MSG) FROM LOG;
Given your sample data and the results you're seeing on different versions of sqlite, here's what I'm sure is happening.
First, you're inserting blobs into your table, not strings. These blobs are stored unchanged, instead of being converted to strings the way numeric values are for a column with TEXT affinity like you're using. See the documentation for details about column affinity and implicit datatype conversions.
Second, the sqlite3 instance that's not matching those blobs was built with the SQLITE_LIKE_DOESNT_MATCH_BLOBS configuration option turned on, and the one that is matching them was built with it turned off (The default setting).
This compile-time option causes the LIKE operator to always return False if either operand is a BLOB. The default behavior of LIKE is that BLOB operands are cast to TEXT before the comparison is done.
If you check PRAGMA compile_options output you should be able to verify that it's being used.
I would paste this into the comments, but the formatting would look ugly. It works for me:
$ sqlite3 log.db
SQLite version 3.24.0 2018-06-04 14:10:15
Enter ".help" for usage hints.
sqlite> create table log (id int, msg text);
sqlite> insert into log values (1,'database is down');
sqlite> insert into log values (2,'database is down');
sqlite> insert into log values (3,'database is down');
sqlite> ^D
$ sqlite3 log.db "select msg from log" | grep down$ | wc -l
3
$ sqlite3 log.db "select msg from log where msg like '%down'" | grep down$ | wc -l
3
Can you share the contents of your log table, so I can try to reproduce your problem?
I would like to edit Firefox cookies using Bash on OS X. I can use the following to convert cookies.sqlite to an ASCII text file:
cd ~/Library/Firefox; sqlite3 cookies.sqlite .dump > test
However I have not yet found a way to convert an edited ASCII text file back to cookies.sqlite. I have tried both dump import and CSV import - sections 8 and 10 on https://www.sqlite.org/cli.html
I suspect the main problem is the format of cookies.sqlite. The following is an example ASCII dump:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, baseDomain TEXT, originAttributes TEXT NOT NULL DEFAULT '', name TEXT, value TEXT, host TEXT, path TEXT, expiry INTEGER, lastAccessed INTEGER, creationTime INTEGER, isSecure INTEGER, isHttpOnly INTEGER, appId INTEGER DEFAULT 0, inBrowserElement INTEGER DEFAULT 0, CONSTRAINT moz_uniqueid UNIQUE (name, host, path, originAttributes));
INSERT INTO moz_cookies VALUES(33,'google.com','','CONSENT','WP.27b523','.google.com','/',2145916800,1561389135468630,1561365552747342,0,0,0,0);
INSERT INTO moz_cookies VALUES(115,'stackoverflow.com','','_gat','1','.stackoverflow.com','/',1561389104,1561389044656946,1561389044656946,0,0,0,0);
INSERT INTO moz_cookies VALUES(117,'stackoverflow.com','','usr','p=[2|6]','stackoverflow.com','/',1577200300,1561389100380300,1561389043655888,1,1,0,0);
INSERT INTO moz_cookies VALUES(120,'google.com','','1P_JAR','2019-06-24-15','.google.com','/',1563981135,1561389135573521,1561365552746756,0,0,0,0);
CREATE INDEX moz_basedomain ON moz_cookies (baseDomain, originAttributes);
COMMIT;
Any assistance would be appreciated.
Just redirect the file to sqlite3's standard input:
sqlite3 cookies.sqlite < test
You'll want to drop existing tables first to avoid all sorts of problems with duplicates though.
Another alternative is to use .read FILENAME from in the sqlite shell.
I have this SQLite table:
create table mytable (
aid INTEGER NOT NULL PRIMARY KEY,
bid INTEGER NOT NULL,
image BLOB
);
And I want to insert a binary file into the image field in this table. Is it possible to do it from the sqlite3 command line interface? If so, how? I'm using Ubuntu.
Thank you!
The sqlite3 command line interface adds the following two “application-defined” functions:
readfile
which typically is used as: INSERT INTO table(blob) VALUES (readfile('myimage.jpg'))
writefile
which writes a file with the contents of a database blob and returns the count of bytes written.
You may use a syntax like :
echo "insert into mytable values(1,1, \"`cat image`\")" | sqlite3 yourDb
i'm not sure for the " around blob's value. Note the backquotes around cat command, means the cat command will be executed before the echo.
[EDIT]
Blob are stored as hexa digit with "X" prefix. You can use "hexdump" unix command to produce the hexa string, but it would be better to write a command line tool that read image and do the insert.
More details on this post : http://comments.gmane.org/gmane.comp.db.sqlite.general/64149
I am using a sqlite database.
I want to excute sqlite from dos command line and insert fields into the tables by passing them as parameters from command line.
C:\run.bat name surname
want to create the run.bat file ...
sqlite > INSERT into tab(col1, col2) Values (%1,%2);
I guess you're looking for something like this:
sqlite3 DATABASENAME 'insert into dbo.TABLE values('%1', '%2')'
You'll need the command line program sqlite3.exe. Then you can put into your batch file:
sqlite3 dbname "INSERT INTO TableName (ColName1, ColName2) VALUES (%1, %2);"
Also note that you will have to be aware of the datatypes of the columns and, if the VALUES for those datatypes require quoting, you'll need to add the quotes into the command line (and handle embedded quotes in the values, if that's a possible occurence).