I've been trying to create a database which contains two tables but for some reason I keep getting this error I've looked at other peoples code and from what I can see its the exact same... I also try to run it one at a time but it only makes one table not both... I may just be missing something small?
Code
import sqlite3
connie = sqlite3.connect('pb.db')
c = connie.cursor()
c.execute("""
CREATE TABLE users(
_ID INTEGER PRIMARY KEY,
username TEXT,
password TEXT
);
CREATE TABLE results(
_ID INTEGER PRIMARY KEY,
users_id INTEGER,
simresult INTEGER,
FOREIGN KEY(users_ID) REFERENCES users(_ID)
);
""")
connie.commit()
Error
Traceback (most recent call last):
File "C:/Users/britsge/OneDrive - Iona College/Year 11/DSO/PowerballSite/db-create-user.py", line 19, in <module>
""")
sqlite3.Warning: You can only execute one statement at a time.
c.execute("""
CREATE TABLE users(
_ID INTEGER PRIMARY KEY,
username TEXT,
password TEXT
);
""")
c.execute("""
CREATE TABLE results(
_ID INTEGER PRIMARY KEY,
users_id INTEGER,
simresult INTEGER,
FOREIGN KEY(users_ID) REFERENCES users(_ID)
);
""")
connie.commit()
Here's a method I created some time ago for a project of mine. You pass in the table name as a string you want to create (calling create_table('table_name')) from your main driving logic and do it this way:
def create_table(ptbl):
""" Assemble DDL (Data Definition Language) Table Create statement and build
sqlite3 db table
Args:
string: new db table name.
Returns:
Status string, '' or 'SUCCESS'.
"""
retval = ''
sqlCmd = ''
try:
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
elif ptbl == 'TBL_VDMS_TEMPLATES_MATRIX':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (ID INTEGER, FILESET TEXT, VDMS TEXT)'
elif ptbl == 'TBL_PLACEHOLDER_MAPPING':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (PLACEHOLDER TEXT, MASTER_FIELD TEXT)'
elif ptbl == 'TBL_VDMS_PLACEHOLDER_MAPPING':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (PLACEHOLDER TEXT, VDMS_FIELD TEXT)'
elif ptbl == 'TBL_DESIGNATIONS':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (GRID TEXT, DESIGNATION TEXT)'
elif ptbl == 'TBL_FILE_MARKER_LINENUMS':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (FILENAME TEXT, LINENO INTEGER, PREFIX_DELIM TEXT, ' \
'MARKER TEXT, SUFFIX_DELIM TEXT)'
elif (ptbl == 'TBL_TEMPLATE_DMS') or (ptbl == 'TBL_TEMPLATE_FEEDER_DMS') or (ptbl ==
'TBL_TEMPLATE_VAULT_DMS'):
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (LINENO INTEGER, LINEVAL TEXT)'
elif ptbl == 'TBL_CABLES':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (ID INTEGER, CABLE_NAME TEXT, CABLE_CONTENT TEXT)'
elif contains(ptbl, '_1_cable'):
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + '(ID INTEGER, S1 TEXT, A1 TEXT, C1 TEXT)'
elif contains(ptbl, '_N_cable'):
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + '(ID INTEGER, SN TEXT, AN TEXT, CN TEXT)'
else:
pass
if sqlCmd != '':
c.execute(sqlCmd)
conn.commit()
conn.close()
retval = 'SUCCESS'
except Error as e:
retval = 'FAIL'
print(e)
return retval
Related
i have two function (in python). The first function defines a new variable which i have to insert in a sql table (first column). The second one, does the same thing, but i want to insert its variable (the second one) near the first variable, so in the second column but in the same line. How can i do with sql?.
connloc = sqlite3.connect("request.db")
sqlloc = "create table requests (" \
" chat_id INTEGER NOT NULL PRIMARY KEY,"\
" locpar varchar(20)," \
" stoppar varchar(20)," \
" locdes varchar(20) ," \
" stopdes varchar(20) );"
connloc.execute(sqlloc)
def name_loc(chat, message):
for i in result:
if message.text == i:
item = [i]
cloc = connloc.cursor()
cloc.execute("INSERT INTO requests(locpar) VALUES (?);", item)
connloc.commit()
def name_stop(chat, message):
for i in result:
for t in result[i]:
if message.text == t:
item = [t]
cloc = connloc.cursor()
cloc.execute("INSERT INTO requests(stoppar) VALUES (?);", item)
connloc.commit()
I would break it up into a two step process by defining two methods, one for table generation and then another second method for populating the new table like this:
def create_table(ptbl):
""" Assemble DDL (Data Definition Language) Table Create statement and build
sqlite3 db table
Args:
string: new db table name.
Returns:
Status string, '' or 'SUCCESS'.
"""
retval = ''
sqlCmd = ''
try:
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
if ptbl == 'TBL_EXAMPLE':
sqlCmd = 'CREATE TABLE IF NOT EXISTS ' + ptbl + ' (FIELD1 TEXT, FIELD2 INTEGER, FIELD3 TEXT, ' \
'FIELD4 TEXT, FIELD5 TEXT)'
else:
pass
if sqlCmd != '':
c.execute(sqlCmd)
conn.commit()
conn.close()
retval = 'SUCCESS'
except Error as e:
retval = 'FAIL'
print(e)
return retval
and then populate it as you like with the values (inserting your new row with those two specific values you mentioned).
Now, I'm populating from a csv file here, but I thinkit'll give you a really good solid start on this task.
def populate_tbl_file_marker_linenums(p_fml_tbl, p_fml_datafile):
""" Read csv and load data into TBL_FILE_MARKER_LINENUMS table ...
Args:
p_fml_tbl (TEXT) target table name
p_fml_datafile (TEXT) name of csv file to load into table
Returns:
retval (TEXT) - Status of method, e.g., 'SUCCESS'
"""
retval = ''
mode = 'r'
try:
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
csv_dataset = open(p_fml_datafile, mode)
csv_reader = csv.reader(csv_dataset)
c.executemany('INSERT INTO ' + p_fml_tbl + ' (FIELD1, FIELD2, FIELD3, FIELD4, FIELD5) VALUES (?, ?, ?, ?, ?)', csv_reader)
conn.commit()
conn.close()
retval = 'SUCCESS'
except Error as e:
print(e)
return retval
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'm new to using sqlite3 with python. I'm having trouble with making the insert_row function in my database class. From the manual on the execute function it appears that I'm doing everything correctly https://docs.python.org/2/library/sqlite3.html. This function does return a number which appears to be correct for lastrowid. However, when I access sqlite3 via command line the table is empty.
def insert_row(self, table, input_dict):
'''
(str, dict) -> int
Add a row to a table in the database
'''
count = 0
col_names = input_dict.keys()
insert_sql = 'INSERT INTO ' + self.sanitize(table) + ' (' + utils.list_to_char_seperated(col_names, ', ', False) + ') VALUES ('
for col in col_names:
count += 1
insert_sql += "'" + self.sanitize(input_dict[col]) + "'"
if count < len(col_names):
insert_sql += ', '
else:
insert_sql += ');'
cur = self.conn.cursor()
cur.execute(insert_sql)
return cur.lastrowid
Accessing via command line.
$ sqlite3
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open blood_cultures.db
sqlite> .tables
vein_draws
sqlite> SELECT * FROM vein_draws;
sqlite>
Also if I take the result of the insert_sql variable and insert it directly into the database via a command line connection this works. So I do not believe it is my insert statement.
Any ideas on what I am doing wrong here?
import re
import sqlite3
from collections import Counter
from string import punctuation
from math import sqrt
# initialize the connection to the database
connection = sqlite3.connect('chatbot.sqlite')
cursor = connection.cursor()
# create the tables needed by the program
create_table_request_list = [
'CREATE TABLE words(word TEXT UNIQUE)',
'CREATE TABLE sentences(sentence TEXT UNIQUE, used INT NOT NULL DEFAULT 0)',
'CREATE TABLE associations (word_id INT NOT NULL, sentence_id INT NOT NULL, weight REAL NOT NULL)',
]
for create_table_request in create_table_request_list:
try:
cursor.execute(create_table_request)
except:
pass
def get_id(entityName, text):
"""Retrieve an entity's unique ID from the database, given its associated text.
If the row is not already present, it is inserted.
The entity can either be a sentence or a word."""
tableName = entityName + 's'
columnName = entityName
cursor.execute('SELECT rowid FROM ' + tableName + ' WHERE ' + columnName + ' = ?', (text,))
row = cursor.fetchone()
if row:
return row[0]
else:
cursor.execute('INSERT INTO ' + tableName + ' (' + columnName + ') VALUES (?)', (text,))
return cursor.lastrowid
def get_words(text):
"""Retrieve the words present in a given string of text.
The return value is a list of tuples where the first member is a lowercase word,
and the second member the number of time it is present in the text."""
wordsRegexpString = '(?:\w+|[' + re.escape(punctuation) + ']+)'
wordsRegexp = re.compile(wordsRegexpString)
wordsList = wordsRegexp.findall(text.lower())
return Counter(wordsList).items()
B = 'Hello!'
while True:
# output bot's message
print('B: ' + B)
# ask for user input; if blank line, exit the loop
H = raw_input('H: ').strip()
if H == '':
break
# store the association between the bot's message words and the user's response
words = get_words(B)
words_length = sum([n * len(word) for word, n in words])
sentence_id = get_id('sentence', H)
for word, n in words:
word_id = get_id('word', word)
weight = sqrt(n / float(words_length))
cursor.execute('INSERT INTO associations VALUES (?, ?, ?)', (word_id, sentence_id, weight))
connection.commit()
# retrieve the most likely answer from the database
cursor.execute('CREATE TEMPORARY TABLE results(sentence_id INT, sentence TEXT, weight REAL)')
words = get_words(H)
words_length = sum([n * len(word) for word, n in words])
for word, n in words:
weight = sqrt(n / float(words_length))
cursor.execute('INSERT INTO results SELECT associations.sentence_id, sentences.sentence, ?*associations.weight/(4+sentences.used) FROM words INNER JOIN associations ON associations.word_id=words.rowid INNER JOIN sentences ON sentences.rowid=associations.sentence_id WHERE words.word=?', (weight, word,))
# if matches were found, give the best one
cursor.execute('SELECT sentence_id, sentence, SUM(weight) AS sum_weight FROM results GROUP BY sentence_id ORDER BY sum_weight DESC LIMIT 1')
row = cursor.fetchone()
cursor.execute('DROP TABLE results')
# otherwise, just randomly pick one of the least used sentences
if row is None:
cursor.execute('SELECT rowid, sentence FROM sentences WHERE used = (SELECT MIN(used) FROM sentences) ORDER BY RANDOM() LIMIT 1')
row = cursor.fetchone()
# tell the database the sentence has been used once more, and prepare the sentence
B = row[1]
cursor.execute('UPDATE sentences SET used=used+1 WHERE rowid=?', (row[0],))
This is a code written for creating a chatbot. When I try running this code on cmd. By using command python chatbot.py, it returns an error saying invalid syntax.
IS there any way i can remove this error and run this code on my system?
it gives error: File "chatbot.py", line 1 syntax: invalid syntax
What version of Python are you running and in what environment? I ran this code on my Python 3.70b4 under Windows and it worked fine except for line 52:
H = raw_input('H: ').strip()
Which you have to change to:
H = input('H: ').strip()
This is probably unrelated directly to your issue, but the code you posted did run fine for me in my environment, after I made that one change (and of course installed any libraries or modules needed).
This is my first post here and I found so many answers here I am really confident, that some can help me. Because I work with Python for only about half a year now, it maybe is a very stupid beginners question. Forgive me so far...
In my project I have several different functions. Two of them are listed below. mysqlLogbookIndex is a thread witch show refresh a list with the names of some table from a database. The connection to the DB is existing at the moment, the function is called and so far it works fine. At the end I can see a tuple containing all the table names.
But now the second function, named create_flight comes in. It is a callback for a Tkinter button and creates some new tables in my database. It uses the same preopened connection than mysqlLogbookIndex.
I expected to see the new tables is my tuple in the next cycle of mysqlLogbookIndex but what happened is that result turned into a None.
Do you know why?
Widget function:
def create_flight(self):
# *********************************************************************************************
# * Create new flight table *
# *********************************************************************************************
if self.sql_write.get():
# ********************************************
# * Try to connect message *
# ********************************************
self.printLog(self.lbl_sql_write, self.LANG['tryCreateTable'], 'normal')
logging.info('Creating table for flight tracking...')
# ********************************************
# * Create name tables *
# ********************************************
event = self.oprSett['mysql']['event']
now = str(int(time.time()))
mainName = "tbl_%s_%s_" % (event, now)
trackingTable = mainName + "flighttrack"
logging.debug('Name of tracking table: %s', trackingTable)
unitsTable = mainName + "units"
logging.debug('Name of units table: %s', unitsTable)
headerTable = mainName + "header"
logging.debug('Name of header table: %s', headerTable)
# ********************************************
# * Read SQL parameter *
# ********************************************
logging.debug('Reading CSV file for table structure...')
csvFile = "config/newFlight.csv"
try:
sqlCsv = csv.reader(open(csvFile, 'rb'),
delimiter = ',',
quotechar = '"',
quoting = csv.QUOTE_ALL
)
except:
msg = csvFile
msg += "\n\n"
msg += self.LANG['e13']
tkMessageBox.showerror("Error 13", msg)
self.printLog(self.lbl_sql_write, self.LANG['e13'], 'error')
logging.error('File not found!')
#print "[Error 13] " + self.LANG['e13']
return 0
# Transfer data from CSV file into own list
sqlVars = []
for row in sqlCsv:
if len(row) == 4 and row[0][0] != "#": # No comment
sqlVars.append(row)
# *************************************************
# * Create SQL statement to create tracking table *
# *************************************************
# Head for creating new table
sql = "CREATE TABLE IF NOT EXISTS `%s` (\n" % trackingTable
sql += " `ID` int(11) NOT NULL AUTO_INCREMENT,\n" # Becomes primary key
# Parse SQL variables from CSV file
for row in sqlVars:
if len(row[2]) > 0: # Data type requires length
sql += " `%s` %s(%s) NOT NULL COMMENT '%s',\n" % (row[0], row[1], row[2], row[3])
else: # Data type not requires length
sql += " `%s` %s NOT NULL COMMENT '%s',\n" % (row[0], row[1], row[3])
# Footer of SQL statement for creating new table
sql += " PRIMARY KEY (`ID`)\n"
sql += ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=0;\n"
sql += "\n"
# In debug mode print SQL statement to console
#logging.debug('SQL statement to create tracking table:\n%s', sql)
# **********************************************
# * Create SQL statement to create units table *
# **********************************************
# Head for creating new table
sql += "CREATE TABLE IF NOT EXISTS `%s` (\n" % unitsTable
sql += " `ID` int(11) NOT NULL AUTO_INCREMENT,\n" # Becomes primary key
sql += " `Dataref` varchar(10) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " `Unit` varchar(10) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " PRIMARY KEY (`ID`)\n"
sql += ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=1 ;\n"
sql += "\n"
# Parse SQL variables from CSV file
for row in sqlVars:
# Insert units in tbl_units
sql += "INSERT INTO %s VALUES ('', '%s', '%s');\n" % (unitsTable, row[0], row[3])
sql += "\n"
# In debug mode print SQL statement to console
#logging.debug('SQL statement to create units table:\n%s', sql)
# ***********************************************
# * Create SQL statement to create header table *
# ***********************************************
# Head for creating new table
sql += "CREATE TABLE IF NOT EXISTS `%s` (\n" % headerTable
#sql += " `ID` int(11) NOT NULL AUTO_INCREMENT,\n" # Becomes primary key
sql += " `Parameter` char(21) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " `Value` varchar(100) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " PRIMARY KEY (`Parameter`)\n"
sql += ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=1 ;\n"
sql += "\n"
# IGC syntax from: http://carrier.csi.cam.ac.uk/forsterlewis/soaring/igc_file_format/igc_format_2008.html
# Adding header parameters, some values are coming later
sql += "INSERT INTO %s VALUES ('AXXX001', '');\n" % (headerTable) # Manufacturer code
sql += "INSERT INTO %s VALUES ('HFFXA', '035');\n" % (headerTable) # Fix accuracy
sql += "INSERT INTO %s VALUES ('HFDTE', '');\n" % (headerTable) # UTC date of flight
sql += "INSERT INTO %s VALUES ('HFPLTPILOT', '');\n" % (headerTable) # Pilots name
sql += "INSERT INTO %s VALUES ('HFGTYGLIDERTYPE', 'KA8B');\n" % (headerTable) # Glider type
sql += "INSERT INTO %s VALUES ('HFGIDGLIDERID', 'D1389');\n" % (headerTable) # Glider callsign
sql += "INSERT INTO %s VALUES ('HFDTM100DATUM', 'WGS-1984');\n" % (headerTable) # GPS datum
sql += "INSERT INTO %s VALUES ('HFGPSGPS', 'X-PLANE 10');\n" % (headerTable) # Manufacturer of GPS module
sql += "INSERT INTO %s VALUES ('HFFTYFRTYPE', 'FLORIANMEISSNER,HCM');\n" % (headerTable) # Logger type
sql += "INSERT INTO %s VALUES ('HFRFWFIRMWAREVERSION', '%s');\n" % (headerTable, self.VERSION) # Firmware version
sql += "INSERT INTO %s VALUES ('HFRHWHARDWAREVERSION', '%s');\n" % (headerTable, self.XPLANEVERSION) # Hardware version
sql += "INSERT INTO %s VALUES ('HFCCLCOMPETITIONCLASS', 'CLUB');\n" % (headerTable) # Competition class
# ********************************************
# * Handover SQL statement to create table *
# * to DB. *
# ********************************************
logging.debug('SQL statement to create all tables:\n%s', sql)
try:
cur = self.conn.cursor()
cur.execute(sql)
cur.close()
except pymysql.Error, e:
tkMessageBox.showerror("Error 9", self.LANG['e9'])
self.printLog(self.lbl_sql_write, self.LANG['e9'], 'error')
#print "[Error 9] " + self.LANG['e9']
logging.error('Could not create tracking table!')
logging.debug(e)
return 0
# ********************************************
# * Print success message *
# ********************************************
logging.info('Tracking table created.')
self.printLog(self.lbl_sql_write, self.LANG['doneCreateTable'], 'success')
text = self.LANG['flightId'] + " " + trackingTable
self.printLog(self.lbl_sql_write, text, 'normal', timestamp=False)
# Make trackingTable public
self.tableName = trackingTable
# Enable tambour register to write in database
self.tambourInMysql = True
Refresh Loop:
def mysqlLogbookIndex(self):
delay = float(self.oprSett['logbook']['refresh_delay']) / 1000
# ********************************************************************
# * Run only if MySQL connection from writeMysql.py is active *
# ********************************************************************
while self.sql_write.get():
# ********************************************
# * Query to get list with tables *
# ********************************************
logging.info('Querying list with tables from database')
dbName = self.oprSett['mysql']['db']
sql = "SELECT TABLE_NAME\n"
sql += "FROM `information_schema`.`TABLES`\n"
sql += "WHERE `TABLE_SCHEMA` LIKE '%s'" % dbName
try:
cur = self.conn.cursor()
cur.execute(sql)
result = cur.fetchall()
cur.close()
except pymysql.Error, e:
logging.warning('Could not take flights from database!')
logging.debug(e)
self.printLog(self.lbl_sql_write, self.LANG['e20'], 'error')
#continue
else:
logging.info('Logbook refreshed.')
print result
Thanks in advance for all helpful posts...
Found the error more or less. It seems to be a MySQL issue.
In my original idea I connected to DB "hcm" and tried to get some table informations from "information_schema" DB. If the "hcm" was changed the query returned None.
Solution:
I established a second independent MySQL connection in my function for the refresh loop and connected to "information_schema" directly.