Sqlite Using Command Line

Run command-line SQLite query and exit

Just include the command in quotes after the database file argument.

For example, the following creates a table called abc:

sqlite3 test.db 'create table abc (col0 int)'

Running a Sqlite3 Script from Command Line

The parameter you give to the sqlite3 program is the database file name.

To execute commands from a file, you must redirect the input to that file:

$ sqlite3 mydatabase.db < SQLTableTransfer

or tell it to read from that file:

$ sqlite3 mydatabase.db ".read SQLTableTransfer"

Opening database file from within SQLite command-line shell

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

Sqlite using command line

Here are two answers for two questions:

  1. I find that using the sqlite3 command line client my arrow keys work without trouble.

  2. You have two errors in your INSERT statement:

    • You should not supply a value for the AUTOINCREMENT column.

    • You should list the columns and the order in which to map the values. This is required when you do not have the same amount of values as columns, but it's good practice even when you do, because later changes to the table's structure may change the order or number of columns.

    • Also, single quotes are more standard in SQL databases. SQLite will accept the double quotes, some other programs won't.

      INSERT INTO Resource (ResourceType) VALUES ('razor')

Creating an sqlite database using command line shell in windows

Here is one way to use SQLite3 from the command prompt in Windows.

First you need to know the path to the folder where you installed the SQLite ODBC Driver. If you used the default settings this would be C:\Program Files\SQLite ODBC Driver on Windows XP

Go to Start -> Run -> type cmd -> click OK

Type cmd

This opens the command prompt.

Command prompt

In the Shell, type in the following command. You can use Alt + Space if you prefer to use cut and paste. Remember to modify the path for your setup if you installed SQLite in another folder.

cd C:\Program Files\SQLite ODBC Driver

Path to folder

This brings you to the SQLite install folder. Now you are ready to call SQLite. Type the following command at the SQLite prompt.

sqlite3

Call SQLite

This opens the File menu where you can choose a database to connect to, or create a new database file.

File menu

Navigate to C:\db\sqlite and create myDatabase.db, and click Open to close the file menu.

Now you are ready to work on your new database, run your queries, e.g. create a table.

Create table

How to run sqlite3 inline command using c#?

As mentioned in the comments, there are two things going on when you run this command from the command prompt, you're running SQLite, but because of the redirect operator, you're also having the shell capture the output and create a file for you.

If you want to recreate this exact behavior, you need to run the shell and pass it this command:

    string sqlLite3ExePath = "cmd";
string sqLitePath2 = "/c \"sqlite3 -header -csv local-DataBase.sqlite ^\"select* from customers;^\" > data.csv\"";

using (var pProcess = new Process())
{
pProcess.StartInfo.FileName = sqlLite3ExePath;
pProcess.StartInfo.Arguments = sqLitePath2;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
pProcess.StartInfo.CreateNoWindow = false;
pProcess.EnableRaisingEvents = true;
pProcess.Exited += PProcess_Exited;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
Console.WriteLine(output);
}


Related Topics



Leave a reply



Submit