How to simulate a corrupt SQLite database - sqlite

Part of the startup routine in my application validates its SQLite databases by using the PRAGMA integrity_check; API. I'm looking for a way to legitimately corrupt an SQLite database so that this "integrity check" will fail.
I've tried messing with the database file in a text editor, but this causes a type of corruption that is caught when opening the database, much earlier than my call to "integrity check".
Any ideas?

PRAGMA writable_schema allows you to change the database schema behind SQLite's back, and thus to violate constraints:
$ sqlite3 test.db
> CREATE TABLE t(x);
> CREATE INDEX tx ON t(x);
> INSERT INTO t VALUES (1), (1);
> PRAGMA writable_schema = 1;
> UPDATE sqlite_master
> SET sql = 'CREATE UNIQUE INDEX tx ON t(x)'
> WHERE type = 'index' AND name = 'tx';
> .quit
$ sqlite3 test.db # reload
> PRAGMA integrity_check;
non-unique entry in index tx

Related

How to use Command Line to access an encrypted SQLite db file instead of DB Browser interface

Currently I can use DB Browser for SQLite to open an encrypted DB file with password on Mac.
The options as per image:
Raw key (start with 0x...)
SQLCipher 3 defaults
I would like to open this file using command line instead of DB Browser.
Tried follow commands but seems no luck so far.
sqlcipher xxx.db
SQLite version 3.37.2 2022-01-06 13:25:41 (SQLCipher 4.5.1 community)
Enter ".help" for usage hints.
sqlite> pragma key="0xMyKey";
ok
sqlite> .tables
Error: file is not a database
Appreciate the help in advance so that I can use command line to access or export sqlite db just like mysql (mysql -u ... & mysqldump).
Found the documentation via this link: https://www.zetetic.net/sqlcipher/sqlcipher-api/
sqlcipher xxx.db
SQLite version 3.37.2 2022-01-06 13:25:41 (SQLCipher 4.5.1 community)
Enter ".help" for usage hints.
sqlite> PRAGMA key = "x'{KEY_WITHOUT_0X}'"; // Replace {KEY_WITHOUT_0X} with your key without 0x!
ok
sqlite> PRAGMA cipher_page_size = 1024;
sqlite> PRAGMA kdf_iter = 64000;
sqlite> PRAGMA cipher_hmac_algorithm = HMAC_SHA1;
sqlite> PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;
sqlite> PRAGMA cipher_default_plaintext_header_size = 0;
sqlite>

Is there a way to find who is the user currently logged in the OS in sqlite3?

For example in DB2 there is a "user" that will give you who is logged in and performing the queries. And no, there is no "logging into DB2" I only login once to gain access to the whole OS (IBM i 7.1).
declare global temporary table XYZ ( DUMMY char(1))
Table XYZ created in QTEMP.
insert into XYZ values('1')
1 rows inserted in XYZ in QTEMP.
select user from XYZ
Output:
....+....1....+...
USER
"my-login-user-id-is-shown-here"
******** End of data ********
in sqlite3:
SQLite version 3.11.1 2016-03-03 16:17:53
Enter ".help" for usage hints.
sqlite> create temporary table XYZ (DUMMY char(1));
sqlite> insert into XYZ values('1')
...> ;
sqlite> select user from XYZ;
Error: no such column: user
sqlite>
Thanks in advance!
SQLite is a file-based database; it neither cares nor knows about OS users.
I tried this (running sqlite from the unix shell) and found it to be an acceptable solution, but I will like to be able to do the same from within the sqlite command shell.
% sqlite3 test.db "select * from NADA where UZER='$USER';"
-- Loading resources from /home/<my-userid>/.sqliterc
UZER DINERO
---------- ----------
<my-userid> 117.41

Encrypting sqlite database with SQLiteCrypt

I am rally struggling with finding solution to protect sqlite database with password. I am trying with SQLiteCrypt. I followed insttruction from a link! , but when i type;
sqlite3_open_v2("data.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
i am getting: Error: near "sqlite3_open_v2": syntax error.
Can someone help?
D:\>sqlite.exe data.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA lic = '77523-009-0000007-72328';
sqlite> PRAGMA rekey = 'ac23';
sqlite> .exit
You MUST exit first to see the effect!
D:\>sqlite.exe data.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from _MapPropertyA;
Error: file is encrypted or is not a database
DONE, your database file encrypted

Why won't RODBC upload a dataframe to SQL Server?

library(RODBC)
con <- odbcDriverConnect("driver=SQL Server; server=name")
df <- data.frame(a=1:10, b=10:1, c=11:20)
Trying to upload the dataframe:
sqlSave(con, df, tablename='[MyDatabase].[MySchema].[MyTable]', rownames=F)
>Error in sqlColumns(channel, tablename) :
‘MyDatabase.MySchema.MyTable’: table not found on channel
..alternatively creating the table first and then appending to it:
cmd <- "create table [MyDatabase].[MySchema].[MyTable] ([a] int, [b] int, [c] int)"
sqlQuery(con, cmd)
sqlSave(con, df, tablename='[MyDatabase].[MySchema].[MyTable]', rownames=F, append=T)
>Error in sqlSave(con, df, tablename = "MyTable", rownames = F, :
42S01 2714 [Microsoft][ODBC SQL Server Driver][SQL Server]There is already an object named MyDatabase.MySchema.MyTable in the database.
[RODBC] ERROR: Could not SQLExecDirect 'CREATE TABLE MyDatabase.MySchema.MyTable ("a" int, "b" int, "c" int)'
What am I doing wrong?
If I add brackets I also get an error.
If I use a connection string with the database to make sure that I am in the correct database (not master) and execute the statement sqlSave(con, df, tablename='dbo.MyTable4', rownames=F) or sqlSave(con, df, tablename='MyTable5', rownames=F) it works.
When connecting to a Microsoft SQL Server, it is essential to use odbcDriverConnect instead of odbcConnect in order to perform sqlSave statements etc. Only odbcDriverConnect allows to specifiy a specific database in the connection string.
RODBC looks at the default folder for the ODBC server connection to see if you have write permissions there (even if you are going for a subdirectory). If you don't have master permissions, then it fails.
I had to create two connections within R, one for reading from the master and one for writing to my temp directory.
These were set by creating two server connections using my local computer's ODBC Administration (within Win7):
-One that defaults to the write-protected master server directory and that I use to pull read-only data.
-One that defaults to the server directory that I have write/drop permissions to.
In other words, your problem is solved by changing your machine's ODBC connection and having R point to the new server connection you will make (the one that defaults to your write-permissioned table.

sqlite file creation

I know this is the stupid question but i am so confused so please help me.
The question is when i use sqlite command line and make a database.
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> sqlite3 mydata.db
---> (now i terminate it using ;)
And now i craete tables and use .output filename and then select * from tb(table)
The file craeted nowhere!!
So please let me know where file craeted.
Or any other method to craete sqlite database file.
Thanks in advance.
What's wrong with this? It perfectly works and both mydatabase.db and foo_contents.txt exist.
C:\Users\··\Desktop>sqlite mydatabase.db
SQLite version 3.7.6.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo(bar text);
sqlite> begin transaction; insert into foo(bar) select 'a' union all select 'b';
sqlite> end transaction;
sqlite> .output '.\foo_contents.txt'
sqlite> select * from foo;
sqlite> .output stdout
sqlite> .q
C:\Users\··\Desktop>
Not sure about your trouble...
fpuga#ELNATH:/tmp$ sqlite3 database.sqlite
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /tmp/database.sqlite
sqlite> .exit
fpuga#ELNATH:/tmp$ ls *.sqlite
database.sqlite

Resources