Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create database and add 2 table in SQLite

Tags:

sqlite

vb.net

I am using SQLite ADO.NET Provider.

I want to create a database with 2 tables via code in vb.net .

Please provide code for the same.

I am using VS 2010 Winforms, working under XP SP3 Pro

like image 861
SpongeBob SquarePants Avatar asked Feb 17 '26 13:02

SpongeBob SquarePants


2 Answers

Use the SQLiteConnection's CreateFile() method.

SQLiteConnection.CreateFile("c:\\mydatabasefile.db3")

More info on the System.Data.SQLite forums

You can then send ad-hoc CREATE TABLE statements to the engine:

dim myTableCreate as String = 
                    "CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC, 
                     FirstName VARCHAR(25));"

cmd.CommandText = myTableCreate
cmd.ExecuteNonQuery()

More on SQLite CREATE TABLE.

like image 140
p.campbell Avatar answered Feb 20 '26 19:02

p.campbell


For those who need this, here is an updated working code...

SQLiteConnection.CreateFile("c:\mydatabasefile.db3")
Using Query As New SQLiteCommand()
    Connection.ConnectionString ="DataSource=c:\mydatabasefile.db3;Version=3;New=False;Compress=True;"
    Connection.Open()
        With Query 
            .Connection = Connection
            .CommandText = "CREATE TABLE MyTable(CustomerID INTEGER PRIMARY KEY ASC, FirstName VARCHAR(25))"
        End With
Query.ExecuteNonQuery()
Connection.Close()
End Using
like image 42
user2866752 Avatar answered Feb 20 '26 18:02

user2866752



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!