dblist = ('database1', 'database2', 'database3', 'database4', 'database5', 'database6', 'database7')
for db in dblist:
cursor = conn.cursor()
cursor.execute("select SYSTEM from automation/awdclient where db = " + "'" + db + "'" + "")
for row in cursor:
activesystem.db = row[0]
cursor.close
conn.close
print activesystem.database1
print activesystem.database2
What I am doing is retrieving a system name from a db. I want to assign a variable equal to that system name, with the variable name system.whatever the db string was.
Use:
activesystem.setattr(db, row[0])
Related
I get this error when adding data to the database.
How do I solve this?
Error:
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'hn' in 'field list'
I know this column does not exist but I am not sending data to such a column anyway.
My Python code:
def addToTable(table_name,connection,column_name_list,*data_list):
if(len(column_name_list) != len(data_list)):
raise ValueError("'column_name_list' length has to be equal to 'data_list' length. Please check the parameters")
cursor = connection.cursor() # initializing a cursor
for column_data in range(len(data_list[0])):
addList = list()
for data in range(len(data_list)):
added = str(data_list[data][column_data])
addList.append(added)
cursor.execute("INSERT INTO " + table_name + " VALUES (" + ", ".join(str(k) for k in addList) + ")")
mydb.commit()
print("Added {} in {} ...".format(added, table_name))
Sample query sent from python code:
INSERT INTO deneme VALUES (hn, 1212, asdmailcom)
calling the function:
names = ["hn","ben","alex",]
numbers = [1212,1245,54541]
mails = ["asdmailcom","fghmailcom","xyzmailcom"]
columns = ["de","ne","me"]
mydb = mysql.connector.connect(host="127.0.0.1",
user="root",
passwd="1234",
database="deneme",
auth_plugin='mysql_native_password')
addToTable("deneme",mydb,columns,names,numbers,mails)
My table name is 'deneme', database name is 'deneme'. Columns : 'de' varchar(45), 'ne' varchar(45), 'me' varchar(45)
I solved the problem. I explained in the comment lines.
def addToTable(table_name,connection,column_name_list,*data_list):
if(len(column_name_list) != len(data_list)):
raise ValueError("'column_name_list' length has to be equal to 'data_list' length. Please check the parameters")
cursor = connection.cursor() # initializing a cursor
for column_data in range(len(data_list[0])):
addList = list()
for data in range(len(data_list)):
added = str(data_list[data][column_data])
added = "'"+added+"'" # the purpose of this line is to convert the data to string
# example: without this line
# query ---> INSERT INTO table_name (column1, column2, ...) VALUES (lorem,ipsum,sit)
# example: with this line
# query ---> INSERT INTO table_name (column1, column2, ...) VALUES ('lorem','ipsum','sit')
addList.append(added)
cursor.execute("INSERT INTO " + table_name + " VALUES (" + ", ".join(str(k) for k in addList) + ")")
mydb.commit()
print("Added {} in {} ...".format(added, table_name))
I am trying to use variables in a python function to try and retrieve attributes with mysql connector
It seems to work only when I specify the name of the attribute in the query itself
def insert(ids, added_attribute):
insert = ''
if len(ids) > 0:
#insert scpecified attributes wanted
insert += ' AND (%s = %s' %(added_attribute, ids[0])
#for loop for more than one specified specific attribute
for id_index in range(1, len(ids)):
insert += ' OR %s = %s' %(added_attribute, ids[id_index])
insert += ')'#close parenthesis on query insert
return insert
def get(name, attributes = 0, ids = []):
cursor = conn.cursor()
#insert specific ids
insert = insert(ids, "id")
query = 'SELECT %s FROM (TABLE) WHERE (name = %s%s)'
cursor.execute(query, (attributes, name, insert))
data = cursor.fetchall()
cursor.close()
return data
I keep getting null as a return value
Try this...
query = 'SELECT {} FROM (TABLE) WHERE (name = {}{})'
cursor.execute(query.format(attributes, name, insert))
{} is replacing %s here and to call the variables you just need to add .format() with the vars you want inserted in order.
I have a large csv file of 30 million rows(1.6 gb) and I am using pymysql to load the data from csv to mysql tables.
I have removed all constraints in table schema to make load faster and have also set timeout values to large values.
def setTimeOutLimit(connection):
try:
with connection.cursor() as cursor:
query = "SET GLOBAL innodb_lock_wait_timeout = 28800"
cursor.execute(query)
query2 = "SET innodb_lock_wait_timeout = 28800"
cursor.execute(query2)
query3 = "SET GLOBAL connect_timeout = 28800"
cursor.execute(query3)
query4 = "SET GLOBAL wait_timeout = 28800"
cursor.execute(query4)
query5 = "SET GLOBAL interactive_timeout = 28800"
cursor.execute(query5)
query6 = "SET GLOBAL max_allowed_packet = 1073741824"
cursor.execute(query6)
except:
conn.close()
sys.exit(" Could not set timeout limit ")
The data gets inserted into the table but I need to make one of the column as Primary Key and so I am creating another table that makes that column primary index by ignoring duplicate values. (tableName_1 is old table tableName is new table)
def createNewTableFromOld(connection, tableName):
try:
pprint( " Creating new table from old table with constraints" )
with connection.cursor() as cursor:
query = (" CREATE TABLE " + tableName +
" Like " + tableName + "_1")
cursor.execute(query)
query2 = (" ALTER TABLE " + tableName +
" ADD PRIMARY KEY(TimeStamp) ")
cursor.execute(query2)
query3 = (" INSERT IGNORE INTO " + tableName +
" SELECT * FROM " + tableName + "_1")
cursor.execute(query3)
query4 = ("DROP TABLE " + tableName + "_1")
cursor.execute(query4)
connection.commit()
except:
conn.close()
sys.exit(" Could not create table with Primary Key ")
During this method execution, somewhere after 5-6 minutes I get this error,
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
And when I check services, MYSQL80 automatically crashed and stopped. I have also set max_allowed_packet_size to 1 gb in my.ini file and all timeouts are manually set to 8 hours. What could be the issue?
The original table schema is:
query = ("CREATE TABLE IF NOT EXISTS " + table + " ("
" TimeStamp DECIMAL(15, 3), " +
" Value DECIMAL(30, 11), " +
" Quality INT, " +
" TagName varchar(30) )"
)
I finally solved the issue by setting the innodb_buffer_pool_size in my.ini file to 2GB which was earlier only 4M.
I am trying to Inserat something from Input into my Database. But getting the Error:
sqlite3.OperationalError: no such column: kundename
import sqlite3
conn = sqlite3.connect('datenbank.db')
print ("Opened database successfully")
kundenname= input("Kundename: ")
auftragstyp= input("Auftragstyp: ")
auftragsurl= input("Auftragsurl: ")
anzahl= input("Anzahl der Bewertungen: ")
conn.execute("INSERT INTO kundenname VALUES (kundename,auftragstyp,auftragsurl,anzahl)", (kundenname, auftragstyp, auftragsurl, anzahl))
conn.commit()
print ("Records created successfully")
conn.close()
But if I make like:
import sqlite3
conn = sqlite3.connect('datenbank.db')
print ("Opened database successfully")
conn = conn.execute("SELECT ID, kundename from kundenname")
for row in conn:
print ("ID = ", row[0])
print ("kundename = ", row[1])
print ("Operation done successfully")
conn.close()
then it works and Shows me the Datas in the Base. But why insert saying the colum dosent excist?
Thank you very much!
I think you have a problem with this line:
conn.execute("INSERT INTO kundenname VALUES
(kundename,auftragstyp,auftragsurl,anzahl)", (kundenname, auftragstyp,
auftragsurl, anzahl))
This is not the way to insert, try this:
conn.execute("INSERT INTO kundenname
('kundename','auftragstyp','auftragsurl','anzahl') VALUES (" +
str(kundename) +"," + str(auftragstyp) + "," + str(auftragsurl) + ","
+ str(anzahl)+")"
The interpreter is complaining about your using unquoted strings. It's interpreting them as variable names in your insert statement. Try this:
conn.execute("INSERT INTO kundenname ('kundename','auftragstyp','auftragsurl','anzahl') VALUES (kundenname, auftragstyp, auftragsurl, anzahl)")
I have several ms access databases that each have a table named PlotStatus-name-3/13/12.
I need to import each of these tables into a .csv table. If I manually change the name of the tables to PlotStatus_name_3_13_12, this code works. Does anyone know how to change the table namees using python?
#connect to access database
for filename in os.listdir(prog_rep_local):
if filename[-6:] == ".accdb":
DBtable = os.path.join(prog_rep_local, filename)
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=' + DBtable)
cursor = conn.cursor()
ct = cursor.tables
for row in ct():
rtn = row.table_name
if rtn[:10] == "PlotStatus":
#this does not work:
#Oldpath = os.path.join(prog_rep_local, filename, rtn)
#print Oldpath
#fpr = Oldpath.replace('-', '_')#.replace("/","_")
#print fpr
#newname = os.rename(Oldpath, fpr) this does not work
#print newname
#spqaccdb = "SELECT * FROM " + newname
#this workds if I manually change the table names in advance
sqlaccdb = "SELECT * FROM " + rtn
print sqlaccdb
cursor.execute(sqlaccdb)
rows = cursor.fetchall()
An easier solution would be to just add brackets around the table name so that the /s don't throw off the SQL command interpreter.
sqlaccdb = "SELECT * FROM [" + rtn + "]"