Jump to content
Sign in to follow this  
ameliabob

Dropping a table

Recommended Posts

In all the examples they all seem to create a table before accessing it. How would I go about accessing one that has already been created the following code

	import sqlite3
	conn = sqlite3.connect('earnings.db')
cur = conn.cursor()
DROP TABLE closed
cur.execute('CREATE TABLE closed (rowId INTEGER PRIMARY KEY AUTOINCREMENT, nature TEXT)
	

This generates a Syntax Error with TABLE highlighted in Red.

If I remove the DROP line then I get the error that the table already exists

?????

Share this post


Link to post
Share on other sites

Do a 'SELECT * FROM closed' .  If no error, it's still there unless corrupted.

closed is not a reserved word, but close is.

 

Share this post


Link to post
Share on other sites

You can't just mix SQL and Python, you have to put the SQL in a string and pass the string into a database method.

Try passing the "DROP TABLE" line as a string into the execute() method. 

Share this post


Link to post
Share on other sites

That's a syntax error because you wrote the SQL code in the middle of your python code.

Share this post


Link to post
Share on other sites

And inside the SQLite shell, you need to import the sqlite3 package?  I'm pretty sure you showed python code there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×