Is there any tool or script available?
I think what you're looking for is:
sqlite x.db .dump > output.sql
This will dump the SQL to regenerate a database
DB Browser for SQLite supports SQL generation.
Go to File
Select Export
Click to Database to SQL file...
A popup screen will be shown.
There you can choose export options and click OK
It'll prompt for a location to save generated sql file.
I hope it'll help the others :)
To generate output into a file follow these steps:
$ sqlite3 mytable.db
sqlite> .output output.sql
sqlite> .dump table_name
sqlite> .quit
If you use the Sqlite manager firefox plugin, it supports bulk inserts from INSERT SQL statements.
Infact it doesn't support this, but Sqlite Browser does (works on Windows, OS X, Linux)
As a quick work around for fewer column tables. We can use string concatenation function. Sample example is-
select 'insert into TABLE_NAME values('''|| COLUMN1 || ''',''' || COLUMN2 || ''',''' || COLUMN3 || ''',''' || COLUMN4 || ''');' from TABLE_NAME
Hope this helps.
http://sqliteadmin.orbmu2k.de/
SQLite Administrator is a powerful tool if you easily want to create, design or administrate SQLite database files. The SQL code editor helps you to quickly write sql queries with features such as code completion and highlighting. Both major versions of SQLite database files are supported by SQLite Administrator.
Features:
* Create / Modify / Delete Tables by Wizard
* Create / Modify / Delete Indices by Wizard
* Create / Modify / Delete Views by Wizard
* Create / Modify / Delete Triggers by Wizard
* SQL Code Completion that supports table aliases
* SQL Code Highlighting
* SQL Error Locating
* Import Data from CSV Files
* Export Data ( XLS / CSV / HTML / XML )
* Store User Queries into Database
* Search for User Queries
* Store Images into Blob Fields ( JPG / BMP )
* Show SQL of each Database Item
* Migrate SQLite2 Databases to SQLite3
* Try to keep Indices and Triggers after modifying a Table
Related
I have 2 SQLite databases with common data but with different purposes and I wanted to avoid reinserting data, so I was wondering if it was possible to copy a whole table from one database to another?
You'll have to attach Database X with Database Y using the ATTACH command, then run the appropriate Insert Into commands for the tables you want to transfer.
INSERT INTO X.TABLE SELECT * FROM Y.TABLE;
// "INSERT or IGNORE" if you want to ignore duplicates with same unique constraint
Or, if the columns are not matched up in order:
INSERT INTO X.TABLE(fieldname1, fieldname2) SELECT fieldname1, fieldname2 FROM Y.TABLE;
Easiest and correct way on a single line:
sqlite3 old.db ".dump mytable" | sqlite3 new.db
The primary key and the columns types will be kept.
Consider a example where I have two databases namely allmsa.db and atlanta.db. Say the database allmsa.db has tables for all msas in US and database atlanta.db is empty.
Our target is to copy the table atlanta from allmsa.db to atlanta.db.
Steps
sqlite3 atlanta.db(to go into atlanta database)
Attach allmsa.db. This can be done using the command ATTACH '/mnt/fastaccessDS/core/csv/allmsa.db' AS AM;
note that we give the entire path of the database to be attached.
check the database list using sqlite> .databases
you can see the output as
seq name file
--- --------------- ----------------------------------------------------------
0 main /mnt/fastaccessDS/core/csv/atlanta.db
2 AM /mnt/fastaccessDS/core/csv/allmsa.db
now you come to your actual target. Use the command
INSERT INTO atlanta SELECT * FROM AM.atlanta;
This should serve your purpose.
For one time action, you can use .dump and .read.
Dump the table my_table from old_db.sqlite
c:\sqlite>sqlite3.exe old_db.sqlite
sqlite> .output mytable_dump.sql
sqlite> .dump my_table
sqlite> .quit
Read the dump into the new_db.sqlite assuming the table there does not exist
c:\sqlite>sqlite3.exe new_db.sqlite
sqlite> .read mytable_dump.sql
Now you have cloned your table.
To do this for whole database, simply leave out the table name in the .dump command.
Bonus: The databases can have different encodings.
Objective-C code for copy Table from a Database to another Database
-(void) createCopyDatabase{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *maindbPath = [documentsDir stringByAppendingPathComponent:#"User.sqlite"];;
NSString *newdbPath = [documentsDir stringByAppendingPathComponent:#"User_copy.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
char *error;
if ([fileManager fileExistsAtPath:newdbPath]) {
[fileManager removeItemAtPath:newdbPath error:nil];
}
sqlite3 *database;
//open database
if (sqlite3_open([newdbPath UTF8String], &database)!=SQLITE_OK) {
NSLog(#"Error to open database");
}
NSString *attachQuery = [NSString stringWithFormat:#"ATTACH DATABASE \"%#\" AS aDB",maindbPath];
sqlite3_exec(database, [attachQuery UTF8String], NULL, NULL, &error);
if (error) {
NSLog(#"Error to Attach = %s",error);
}
//Query for copy Table
NSString *sqlString = #"CREATE TABLE Info AS SELECT * FROM aDB.Info";
sqlite3_exec(database, [sqlString UTF8String], NULL, NULL, &error);
if (error) {
NSLog(#"Error to copy database = %s",error);
}
//Query for copy Table with Where Clause
sqlString = #"CREATE TABLE comments AS SELECT * FROM aDB.comments Where user_name = 'XYZ'";
sqlite3_exec(database, [sqlString UTF8String], NULL, NULL, &error);
if (error) {
NSLog(#"Error to copy database = %s",error);
}
}
The Easiest way to do is through SQLite Studio
If you don't have download from https://download.cnet.com/SQLiteStudio/3000-10254_4-75836135.html
Steps:
1.Add both the databases.
2.Click View tab and then databases as shown in the picture.
3.Right click the table you want to copy and copy it.
Paste the table after right clicking the database where you want to paste.
Now you're done
First scenario: DB1.sqlite and DB2.sqlite have the same table(t1), but DB1 is more "up to date" than DB2. If it's small, drop the table from DB2 and recreate it with the data:
> DROP TABLE IF EXISTS db2.t1; CREATE TABLE db2.t1 AS SELECT * FROM db1.t1;
Second scenario: If it's a large table, you may be better off with an INSERT if not exists type solution. If you have a Unique Key column it's more straight forward, otherwise you'd need to use a combination of fields (maybe every field) and at some point it's still faster to just drop and re-create the table; it's always more straight forward (less thinking required).
THE SETUP: open SQLite without a DB which creates a temporary in memory main database, then attach DB1.sqlite and DB2.sqlite
> sqlite3
sqlite> ATTACH "DB1.sqlite" AS db1
sqlite> ATTACH "DB2.sqlite" AS db2
and use .databases to see the attached databases and their files.
sqlite> .databases
main:
db1: /db/DB1.sqlite
db2: /db/DB2.sqlite
I needed to move data from a sql server compact database to sqlite, so using sql server 2008 you can right click on the table and select 'Script Table To' and then 'Data to Inserts'. Copy the insert statements remove the 'GO' statements and it executed successfully when applied to the sqlite database using the 'DB Browser for Sqlite' app.
If you use DB Browser for SQLite, you can copy the table from one db to another in following steps:
Open two instances of the app and load the source db and target db side by side.
If the target db does not have the table, "Copy Create Statement" from the source db and then paste the sql statement in "Execute SQL" tab and run the sql to create the table.
In the source db, export the table as a CSV file.
In the target db, import the CSV file to the table with the same table name. The app will ask you do you want to import the data to the existing table, click yes. Done.
The only thing I don't have an automated tool for when working with Oracle is a program that can create INSERT INTO scripts.
I don't desperately need it so I'm not going to spend money on it. I'm just wondering if there is anything out there that can be used to generate INSERT INTO scripts given an existing database without spending lots of money.
I've searched through Oracle with no luck in finding such a feature.
It exists in PL/SQL Developer, but errors for BLOB fields.
Oracle's free SQL Developer will do this:
http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html
You just find your table, right-click on it and choose Export Data->Insert
This will give you a file with your insert statements. You can also export the data in SQL Loader format as well.
You can do that in PL/SQL Developer v10.
1. Click on Table that you want to generate script for.
2. Click Export data.
3. Check if table is selected that you want to export data for.
4. Click on SQL inserts tab.
5. Add where clause if you don't need the whole table.
6. Select file where you will find your SQL script.
7. Click export.
Use a SQL function (I'm the author):
https://github.com/teopost/oracle-scripts/blob/master/fn_gen_inserts.sql
Usage:
select fn_gen_inserts('select * from tablename', 'p_new_owner_name', 'p_new_table_name')
from dual;
where:
p_sql – dynamic query which will be used to export metadata rows
p_new_owner_name – owner name which will be used for generated INSERT
p_new_table_name – table name which will be used for generated INSERT
p_sql in this sample is 'select * from tablename'
You can find original source code here:
http://dbaora.com/oracle-generate-rows-as-insert-statements-from-table-view-using-plsql/
Ashish Kumar's script generates individually usable insert statements instead of a SQL block, but supports fewer datatypes.
I have been searching for a solution for this and found it today. Here is how you can do it.
Open Oracle SQL Developer Query Builder
Run the query
Right click on result set and export
http://i.stack.imgur.com/lJp9P.png
You might execute something like this in the database:
select "insert into targettable(field1, field2, ...) values(" || field1 || ", " || field2 || ... || ");"
from targettable;
Something more sophisticated is here.
If you have an empty table the Export method won't work. As a workaround. I used the Table View of Oracle SQL Developer. and clicked on Columns. Sorted by Nullable so NO was on top. And then selected these non nullable values using shift + select for the range.
This allowed me to do one base insert. So that Export could prepare a proper all columns insert.
If you have to load a lot of data into tables on a regular basis, check out SQL Loader or external tables. Should be much faster than individual Inserts.
You can also use MyGeneration (free tool) to write your own sql generated scripts. There is a "insert into" script for SQL Server included in MyGeneration, which can be easily changed to run under Oracle.
I am using Visual Foxpro (Vfp9).
I have stored sqlite db file on my server.
I want to access it from vfp like php does.
How I can achieve this.
Please help.
Thanks in advance.
First you need to get and install SQLite ODBC driver if you haven't done so yet:
SQLite ODBC driver download
Then it is easy. You simply use one of the techniques accessing external data (SQL pass through, Remote views, Cursor adapter). ie: With SQL Pass Through:
Local dbName, handle, lcSQL, ix, cSample
dbName = 'd:\temp\MyDb.s3db'
handle = Sqlstringconnect( Textmerge("driver={SQLite3 ODBC Driver};Database=<< m.dbName >>") )
SQLExec(m.handle, "create table mySampleTable (id int primary key, dummy varchar(50))")
TEXT to lcSQL noshow
insert into mySampleTable
(id, dummy)
values
(?m.ix, ?m.cSample)
ENDTEXT
For ix = 1 To 10
cSample = 'Dummy no ' + Ltrim(Str(m.ix))
SQLExec(m.handle, m.lcSQL)
Endfor
SQLExec(m.handle, 'select * from mySampleTable','sample')
SQLDisconnect(0)
Select Sample
Browse
Note: Normally when inserting in a loop, you would use SQLPrepare() and transaction for performance. I didn't care about that here.
I'm using the SQLite Command Line Shell. As documented, I can open a database by supplying it as an argument to the executable:
sqlite3 data.db
I cannot figure out how to open a database file from within the tool after having invoked it without supplying the file as a command-line argument (if I, say, double-click sqlite3.exe in Windows).
What is the command within the SQLite shell tool to specify a database file?
You can attach one and even more databases and work with it in the same way like using sqlite dbname.db
sqlite3
:
sqlite> attach "mydb.sqlite" as db1;
and u can see all attached databases with
.databases
where in normal way the main is used for the command-line db
.databases
seq name file
--- --------------- ----------------------------------------------------------
0 main
1 temp
2 ttt c:\home\user\gg.ite
I think the simplest way to just open a single database and start querying is:
sqlite> .open "test.db"
sqlite> SELECT * FROM table_name ... ;
Notice: This works only for versions 3.8.2+
The command within the Sqlite shell to open a database is .open
The syntax is,
sqlite> .open dbasename.db
If it is a new database that you would like to create and open, it is
sqlite> .open --new dbasename.db
If the database is existing in a different folder, the path has to be mentioned like this:
sqlite> .open D:/MainFolder/SubFolder/...database.db
In Windows Command shell, you should use '\' to represent a directory, but in SQLite directories are represented by '/'.
If you still prefer to use the Windows notation, you should use an escape sequence for every '\'
The same way you do it in other db system, you can use the name of the db for identifying double named tables. unique tablenames can used directly.
select * from ttt.table_name;
or if table name in all attached databases is unique
select * from my_unique_table_name;
But I think the of of sqlite-shell is only for manual lookup or manual data manipulation and therefor this way is more inconsequential
normally you would use sqlite-command-line in a script
You can simply specify the database file name in the command line:
bash-3.2 # sqlite3 UserDb.sqlite
SQLite version 3.16.2 2017-01-06 16:32:41
Enter ".help" for usage hints.
sqlite> .databases
main: /db/UserDb.sqlite
sqlite> .tables
accountLevelSettings genres syncedThumbs
collectionActivity recordingFilter thumbs
contentStatus syncedContentStatus
sqlite> select count(*) from genres;
10
Moreover, you can execute your query from the command line:
bash-3.2 # sqlite3 UserDb.sqlite 'select count(*) from genres'
10
You could attach another database file from the SQLite shell:
sqlite> attach database 'RelDb.sqlite' as RelDb;
sqlite> .databases
main: /db/UserDb.sqlite
RelDb: /db/RelDb_1.sqlite
sqlite> .tables
RelDb.collectionRelationship contentStatus
RelDb.contentRelationship genres
RelDb.leagueRelationship recordingFilter
RelDb.localizedString syncedContentStatus
accountLevelSettings syncedThumbs
collectionActivity thumbs
The tables from this 2nd database will be accessible via prefix of the database:
sqlite> select count(*) from RelDb.localizedString;
2442
But who knows how to specify multiple database files from the command line to execute the query from the command line?
create different db files using
>sqlite3 test1.db
sqlite> create table test1 (name text);
sqlite> insert into test1 values('sourav');
sqlite>.exit
>sqlite3 test2.db
sqlite> create table test2 (eid integer);
sqlite> insert into test2 values (6);
sqlite>.exit
>sqlite
SQLite version 3.8.5 2014-06-04 14:06:34
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open test1.db
sqlite> select * from test1;
sourav
sqlite> .open test2.db
sqlite> select * from test1;
Error: no such table: test1
sqlite> select * from test2;
6
sqlite> .exit
>
Thank YOU.
Older SQLite command-line shells (sqlite3.exe) do not appear to offer the .open command or any readily identifiable alternative.
Although I found no definitive reference it seems that the .open command was introduced around version 3.15. The SQLite Release History first mentions the .open command with 2016-10-14 (3.15.0).
I wonder why no one was able to get what the question actually asked. It stated What is the command within the SQLite shell tool to specify a database file?
A sqlite db is on my hard disk E:\ABCD\efg\mydb.db. How do I access it with sqlite3 command line interface? .open E:\ABCD\efg\mydb.db does not work. This is what question asked.
I found the best way to do the work is
copy-paste all your db files in 1 directory (say E:\ABCD\efg\mydbs)
switch to that directory in your command line
now open sqlite3 and then .open mydb.db
This way you can do the join operation on different tables belonging to different databases as well.
In my case, I wanted to open a database from another drive by providing the path as a parameter, but it wasn't working. The solution is to wrap the full path to the db in double quotes. So from Powershell window in the folder containing your sqlite3.exe:
.\sqlite3.exe "E:\ABCD\efg\mydb.db"
How can i edit .sqlite file ? Can we convert it to convert to readable text format ?
Here is file link.. http://FastFreeFileHosting.com/file/52328/uploads-sqlite.html
You need the SQLite 2.x command line shell for your operating system to manipulate your uploads.sqlite file.
You can use it to get a full database dump as an SQL transaction:
$ sqlite uploads.sqlite .dump
BEGIN TRANSACTION;
CREATE TABLE 'temp' (
hash text,
file_id integer,
file_name text,
user_info text,
date integer
);
INSERT INTO temp VALUES('248283734d02fac7197b02b3cea7b25c',1,'blocklist.xml','199.27.128.60',20101207124952);
.
.
.
INSERT INTO temp VALUES('10f50e1f9266180306153b900233bdcd',20,'Joku.sis','175.40.26.96',20110103015449);
CREATE TABLE 'uploads' (
id integer(32) not null primary key unique,
filename text(100),
date integer,
user_info text,
hash text
);
INSERT INTO uploads VALUES(1,'blocklist.xml',20101207124943,'199.27.128.60','82d69cf46c45760176f7b214a5cf36b1');
.
.
.
INSERT INTO uploads VALUES(20,'Joku.sis',20110103015402,'175.40.26.96','43a61da540a8e97fedb180c8984a4d3b');
COMMIT;
You can also perform specific queries or updates using SQL:
$ sqlite uploads.sqlite
SQLite version 2.8.17
Enter ".help" for instructions
sqlite> INSERT INTO uploads VALUES(21,'Joku.sis2',20110102015402,'175.40.26.97','43a61da540a8e97fedb180c8984a4d3b');
sqlite> SELECT * FROM uploads WHERE date > 20101224000000;
19|tab-view.zip|20101230002321|27.97.29.98|5a9e7ff82c5a424fe5a19d97079b6dc7
20|Joku.sis|20110103015402|175.40.26.96|43a61da540a8e97fedb180c8984a4d3b
21|Joku.sis2|20110102015402|175.40.26.97|43a61da540a8e97fedb180c8984a4d3b
sqlite>
you could use a shell script to create an HTML or CSV file of the contents of the SQLite database. Will work in any OS (in windows, you will need to install some additional tools).
See Using sqlite3 in a shell script from Command Line Shell For SQLite from the SQLite website. Or you could use a GUI tool.