Syntax Error near "CHANGE" in sqlite3 - python

I am attempting to execute the following (move a column to be the first one)
import sqlite3
db = sqlite3.connect('adatabase.sqlite')
c = db.cursor()
c.execute('ALTER TABLE tab1 CHANGE COLUMN r r def FIRST')
Unfortunately I get this error
Traceback (most recent call last):
File "<input>", line 1, in <module>
OperationalError: near "CHANGE": syntax error
What could be? Thanks in advance

SQLite does not support a CHANGE COLUMN feature; if any.
Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE
command are supported
See all missing features: SQL Features That SQLite Does Not Implement

Related

sqlalchemy cx_oracle cannot get results

sqlalchemy+cx_Oracle may not be in your domain.
However, if you can help me giving few web links/helps will be nice.
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
import cx_Oracle
engine = create_engine('oracle+cx_oracle://user:passwd#FTSDBLAB')
meta = MetaData()
meta.reflect(bind=engine)
tbl_mgr_theater = Table('mgr_table', meta, autoload=True, autoload_with=engine)
connection = engine.connect()
result = connection.execute(tbl_mgr_theater.select())
print(result.rowcount())
gives the following ERROR:
Traceback (most recent call last):
File "", line 1, in
TypeError: 'int' object is not callable
Error closing cursor
Traceback (most recent call last):
AttributeError: 'cx_Oracle.Cursor' object has no attribute 'lastrowid'
First of all, rowcount is an attribute, so you sould use in this way:
print(result.rowcount)
But it will return 0. Why?
Because is only useful in an UPDATE or DELETE statement. Contrary to what the Python DBAPI says, it does not return the number of rows available from the results of a SELECT statement as DBAPIs cannot support this functionality when rows are unbuffered.
How can I get the rowcount of a SELECT statement?
You can SELECT with COUNT in this way:
result = connection.execute(tbl_mgr_theater.select().count())
It will return a ResultProxy. But if you want an int result, you could do:
result=[x for x in connection.execute(tbl_mgr_theater.select().count())][0][0]
As you know is a SELECT COUNT statement (it will only return one field), you could set the first [0], the second is to parse the RowProxy to int.
Hope it helps you.

how to insert a numpy array having only one element to mysql db?

i am having a python program which has got a numpy array with a single element say a=['this is the element']
i need to insert this value to my database(MySQL).
i used execute() but it is showing an attribute error
my sql part to select data from table is this :
import pandas as pd
import mysql.connector
mysql_cn=mysql.connector.connect(user='root', password='',
host='127.0.0.1',port='3388',
database='proj')
X = pd.read_sql('select stamp from test;', con=mysql_cn)
mysql_cn.close()
so what has to be done since mysql_cn.execute() is not working?
i also tried pd.to_sql but it is also not working
this is the error for execute()
Traceback (most recent call last):
File "C:\Python34\data\test.py", line 132, in <module>
haa=mysql_cn.execute('select stamp from test;')// this is my simple query not my actual query
AttributeError: 'MySQLConnection' object has no attribute 'execute'
You need a cursor. You have to create a cursor from your mysql_cn then you can call execute on the cursor.
cursor = mysql_cn.cursor()
# Now call cursor.execute ....

Python SQLite3 Execution Error

Using python 2.7.5, I've written the following code down to compile based off of an online course I'm taking that shows how sqlite3 works with python
import sqlite3 as sql
database1 = sql.connect('test1.db')
db1_cursor = database1.cursor()
cmd = 'CREATE TABLE IF NOT EXISTS users(username TEXT,password TEXT)'
cmd2 = 'INSERT INTO users(username,password) VALUES("testuser,testpassword")'
cmd3 = 'SELECT username,password FROM users'
db1_cursor.execute(cmd)
db1_cursor.execute(cmd2)
db1_cursor.execute(cmd3)
database1.commit()
for x in db1_cursor:
print(x)
Now, upon running this code it gives me the following operation error:
Traceback (most recent call last):
File "C:\Users\Ryan\My Code Projects\Learning\udemycourse.py", line 11, in <module>
db1_cursor.execute(cmd2)
OperationalError: 1 values for 2 columns
Why does it give this error for db1_cursor.execute(cmd2) but not for db1_cursor.execute(cmd1) and how can I fix this?
I think you meant
Values ("testuser","testpassword")
A better way of doing the insert would be
cmd2 = 'INSERT INTO users(username,password) VALUES(?, ?)'
Creating placeholders
db1_cursor.execute(cmd2, ('testuser','testpassword'))
and passing the values as a tuple

Error in loading database entries into lists

I'm getting the following error:
Traceback (most recent call last):
File "/home/pi/Nike/test_two.py", line 43, in <module>
do_query()
File "/home/pi/Nike/test_two.py", line 33, in do_query
for(Product,Bin,Size,Color) in records:
ValueError: too many values to unpack
Code:
def do_query():
connection = sqlite3.connect('test_db.db')
cursor = connection.cursor()
cursor.execute("SELECT * FROM TESTER ORDER BY CheckNum")
records = cursor.fetchall()
for(Product,Bin,Size,Color) in records:
row_1.append(Product)
row_2.append(Bin)
row_3.append(Size)
row_4.append(Color)
connection.commit()
cursor.close()
connection.close()
do_query()
I'm trying to load each column of a table into seperate python list. I am using Python, and sqlite3. Why am I getting this error?
You are using "SELECT *" which will return every column from the table. My guess is that the table in question contains more columns then the 4 you specified.
A better way would actually be specifying in the SQL which columns you want so that your code will not break if columns are added to the database.
Something like "SELECT col1, col2 FROM table"
You can run the sqlite3 tool on the db file and then view the table schema with ".schema <table_name>"

SQLite error in Python

I have a simple piece of code to update a row in sqlite:
def UpdateElement(new_user,new_topic):
querycurs.execute('''INSERT into First_Data (topic) values(?) WHERE user = (?)''',([new_topic], [new_user]))
However, this gives me the error:
Traceback (most recent call last):
File "C:/Python27/Database.py", line 40, in <module>
UpdateElement("Abhishek Mitra","Particle Physics")
File "C:/Python27/Database.py", line 36, in UpdateElement
querycurs.execute('''INSERT into First_Data (topic) values(?) WHERE user = (?)''',([new_topic],[new_user]))
OperationalError: near "WHERE": syntax error
You should be using an UPDATE statement instead of INSERT:
def UpdateElement(new_user,new_topic):
querycurs.execute('''UPDATE First_Data
SET topic = ?
WHERE user = ?''', (new_topic, new_user))
The problem arises from the use of the parentheses and sending in new_user as an array, I believe. Values is an array, user is not.
You want something like:
cur.execute("UPDATE table SET value=? WHERE name=?", (myvalue, myname))
But yes, UPDATE sounds like what you wanted in the first place.

Categories