0

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)")
9
  • 1
    Where do you use .tables? Commented Jun 6, 2017 at 8:58
  • There should be a master table in your database containing info on the tables the database contains, the variable types, etc. Try querying that one for names if I remember correctly Commented Jun 6, 2017 at 9:01
  • i open sqlite3 and use .tables there Commented Jun 6, 2017 at 9:03
  • So the code that gives you the error is not the code you have posted here. This is a problem. Post the code that fails. Commented Jun 6, 2017 at 9:06
  • or i will run above commands in python 2.7 and after that if i want to see that it is create or not, i use .tables. THere it gives me invalid syntex Commented Jun 6, 2017 at 9:06

1 Answer 1

1

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)]
Sign up to request clarification or add additional context in comments.

8 Comments

In terminal if write the codes its working! But when i write this editor and run the file in terminal, it doesnt work. import sqlite3 conn = sqlite3.connect('raman.db') c = conn.cursor() c.execute("SELECT * FROM sqlite_master WHERE type='table'").fetchall() conn.commit() conn.close()
please note that you have already created the table in raman.db, try with a new db like raman1.db , or drop the table.
actually it should shoe me the table , as i ryn this command it gives nothing. i am writing in gedit editor.
Okay. That's perfect. Learned to drop also
Now I have added 4 columns in my table. c.execute("CREATE table Raman(ATOMIC NUMBER INT, SYMBOL TEXT, ROW INT , COLUMN INT)") and I want to insert values for them in the table so I am writing c.execute("INSERT INTO new VALUES(1,'H',1,'1')"); After this if I print the table its not showing me the inserted values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.