简体   繁体   中英

Questions about create table in sqlite3 of python

I am using sqlite3 with Python 2.7. I am learning how to create table in a database, so when I want to see whether it is created or not I use command .tables but that gives me an error:

invalid syntax

Here is the code

import sqlite3
conn = sqlite3.connect('raman.db')
c = conn.cursor()
c.execute("CREATE table new(ID INT NOT NULL)")

Just execute:

c.execute("SELECT * FROM sqlite_master WHERE type='table'").fetchall()

It will give you tables infor:

[(u'table', u'new', u'new', 2, u'CREATE TABLE new(ID INT NOT NULL)')]

Update: Put below code in you py file:

import sqlite3
conn = sqlite3.connect('raman.db')
c = conn.cursor()
c.execute("CREATE table new(ID INT NOT NULL)")
print c.execute("SELECT * FROM sqlite_master WHERE type='table'").fetchall()  #check table info new
c.execute("CREATE table Raman(ATOMIC NUMBER INT, SYMBOL TEXT, ROW INT , COLUMN INT)")
c.execute("INSERT INTO Raman VALUES(1,'H',1,'1')")
conn.commit()
print c.execute("select * from Raman").fetchall() #get data from table Raman
conn.close()

Run the py file in your terminal, it will print:

[(u'table', u'new', u'new', 2, u'CREATE TABLE new(ID INT NOT NULL)')]
[(1, u'H', 1, 1)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM