xampp - python 3.7 - mysql error 1146 table not found - python
File: init.py
import sys
import time
import importlib
create_database = importlib.import_module("create-database")
from settings import *
create_database.import_tables_structure()
time.sleep(2)
import_settings()
time.sleep(2)
File: create-database.py
import mysql.connector
import sys
import os
import traceback
def import_tables_structure():
try:
cnx = mysql.connector.connect(user='root', password='',host='localhost')
cursor = cnx.cursor()
sql_file = open("papinhio-player.sql","r",encoding="utf-8").read()
cursor.execute(sql_file,multi=True)
cnx.commit()
cursor.close()
cnx.close()
except Exception as e:
error_message = traceback.format_exc()
print(error_message)
#make folders
try:
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\history-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\listeners-statistics-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\schedule-transmition-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\week-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\records\\ip-calls"))
except:
pass
#import_tables_structure()
File: papinhio-player.sql
-- phpMyAdmin SQL Dump
-- version 5.1.1deb5ubuntu1
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Aug 02, 2022 at 07:57 PM
-- Server version: 10.6.7-MariaDB-2ubuntu1.1
-- PHP Version: 8.1.2
SET SQL_MODE = "";
START TRANSACTION;
--
-- Database: `papinhio-player`
--
DROP DATABASE IF EXISTS `papinhio-player`;
CREATE DATABASE IF NOT EXISTS `papinhio-player` COLLATE utf8_general_ci;
-- --------------------------------------------------------
-- Table: settings
-- Used for saving program settings
-- such as auto_dj value (0,1)
-- Easy table (only 3 columns)
CREATE TABLE `papinhio-player`.`settings` (
`id` int(11) NOT NULL,
`setting` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Indexes for table `settings`
--
ALTER TABLE `papinhio-player`.`settings`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `setting` (`setting`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `settings`
--
ALTER TABLE `papinhio-player`.`settings`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--
COMMIT;
File: settings.py
import os
import sys
import mysql.connector
def import_settings():
system_sound_volume = 50
#default_font = "Times New Roman"
default_font = "Calibri"
default_font_size = 18
default_font_color = "#000000"
default_background_color = "#F1F1F1"
default_buttons_background = "#F5F5F5"
default_buttons_font_color = "#000000"
#default_style = "WindowsVista"
default_style = "Fusion"
#default_custome_theme = "light_blue.xml"
default_custome_theme = ""
sql = """ INSERT INTO `settings` (`keyword`, `current_value`) VALUES ( ?, ?) """
settings = []
settings.append(("input_device_sound_volume",100))
settings.append(("input_device_normalize",0))
settings.append(("input_device_pan",0))
settings.append(("input_device_low_frequency",20))
settings.append(("input_device_high_frequency",20000))
settings.append(("general_deck_sound_volume",50))
settings.append(("general_deck_normalize",0))
settings.append(("general_deck_pan",0))
settings.append(("general_deck_low_frequency",20))
settings.append(("general_deck_high_frequency",20000))
settings.append(("player_list_display","Προβολή λίστας"))
settings.append(("deck_1_relative_type",""))
settings.append(("deck_1_relative_number",0))
settings.append(("deck_1_current_duration_milliseconds",0))
settings.append(("deck_1_repeats",0))
settings.append(("deck_1_status",0))
settings.append(("deck_1_total_time_milliseconds",0))
settings.append(("deck_2_relative_type",""))
settings.append(("deck_2_relative_number",0))
settings.append(("deck_2_current_duration_milliseconds",0))
settings.append(("deck_2_repeats",0))
settings.append(("deck_2_status",0))
settings.append(("deck_2_total_time_milliseconds",0))
settings.append(("music_clip_deck_relative_type",""))
settings.append(("music_clip_deck_relative_number",0))
settings.append(("music_clip_deck_current_duration_milliseconds",0))
settings.append(("music_clip_deck_repeats",0))
settings.append(("music_clip_deck_status",0))
settings.append(("music_clip_deck_total_time_milliseconds",0))
settings.append(("default_font",default_font))
settings.append(("default_font_size",default_font_size))
settings.append(("default_font_color",default_font_color))
settings.append(("default_background_color",default_background_color))
settings.append(("default_button_background",default_buttons_background))
settings.append(("default_button_font_color",default_buttons_font_color))
settings.append(("default_style",default_style))
settings.append(("default_custome_theme",default_custome_theme))
settings.append(("player_field_change_position","1"))
settings.append(("player_field_play","1"))
settings.append(("player_field_title","1"))
settings.append(("player_field_last_play","1"))
settings.append(("player_field_next_play","1"))
settings.append(("player_field_image","1"))
settings.append(("player_field_prepare","1"))
settings.append(("player_field_play_now","1"))
settings.append(("player_field_remove","1"))
settings.append(("player_field_duration","1"))
settings.append(("player_field_artist","1"))
settings.append(("player_field_album","1"))
settings.append(("player_field_author","1"))
settings.append(("player_field_composer","1"))
settings.append(("player_field_year","1"))
settings.append(("player_field_description","1"))
settings.append(("player_field_from","1"))
settings.append(("player_field_rating","1"))
settings.append(("player_field_volume","1"))
settings.append(("player_field_normalize","1"))
settings.append(("player_field_pan","1"))
settings.append(("player_field_frequencies","1"))
settings.append(("player_field_repeat","1"))
settings.append(("player_field_open_file","1"))
settings.append(("player_fade_in","0"))
settings.append(("player_fade_out","0"))
settings.append(("program_component_tool_bar","1"))
settings.append(("program_component_time_lines","1"))
settings.append(("program_component_general_deck","1"))
settings.append(("program_component_deck_1","1"))
settings.append(("program_component_deck_2","1"))
settings.append(("program_component_music_clip_deck","1"))
settings.append(("program_component_speackers_deck","1"))
settings.append(("program_component_ip_calls","0"))
settings.append(("program_component_player_list","1"))
settings.append(("program_component_web_sites","1"))
settings.append(("program_component_scheduled_transmitions","1"))
settings.append(("repeat_player_list","1"))
settings.append(("auto_dj","1"))
settings.append(("current-working-directory",os.path.abspath(".")))
cnx = mysql.connector.connect(user='root', password='',host='localhost',database='papinhio-player')
cursor = cnx.cursor()
cursor.execute("SET GLOBAL sql_mode='';")
cnx.commit()
cursor.execute("USE `papinhio-player`;")
cnx.commit()
for setting in settings:
sql = "INSERT INTO `papinhio-player`.`settings` (`setting`,`value`) VALUES (%s, %s);"
cursor.execute(sql,(str(setting[0]),str(setting[1])))
cnx.commit()
cursor.close()
cnx.close()
If i run init.py the output is:
Traceback (most recent call last):
File "C:\Users\chris\My Projects\papinhio-player\database\create-database.py",
line 12, in import_tables_structure
cnx.commit()
File "C:\python\lib\site-packages\mysql\connector\connection_cext.py", line 42
5, in commit
self._cmysql.commit()
_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this c
ommand now
Traceback (most recent call last):
File "C:\python\lib\site-packages\mysql\connector\connection_cext.py", line 53
8, in cmd_query
query_attrs=self._query_attrs)
_mysql_connector.MySQLInterfaceError: Table 'papinhio-player.settings' doesn't e
xist
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "init_2.py", line 10, in <module>
import_settings()
File "C:\Users\chris\My Projects\papinhio-player\database\settings.py", line 1
66, in import_settings
cursor.execute(sql,(str(setting[0]),str(setting[1])))
File "C:\python\lib\site-packages\mysql\connector\cursor_cext.py", line 271, i
n execute
raw_as_string=self._raw_as_string)
File "C:\python\lib\site-packages\mysql\connector\connection_cext.py", line 54
1, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'papinhio-player.se
ttings' doesn't exist
the database and table structure are created, but no insertion to settings table.
Then if i run python settings.py the insertions are done.
What's the problem?
Note: I tried multi=True in multi-query but then there is no database build in phpmyadmin.
File: create-database.py
import mysql.connector
import sys
import os
import traceback
import re
def import_tables_structure():
try:
cnx = mysql.connector.connect(user='root', password='',host='localhost')
cursor = cnx.cursor()
exec_sql_file(cursor, "papinhio-player.sql")
except Exception as e:
error_message = traceback.format_exc()
print(error_message)
#make folders
try:
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\history-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\listeners-statistics-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\schedule-transmition-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\reports\\week-report"))
os.mkdir(os.path.abspath("C:\\Users\\chris\\My Projects\\papinhio-player\\disket-box\\records\\ip-calls"))
except:
pass
def exec_sql_file(cursor, sql_file):
statement = ""
for line in open(sql_file,"r",encoding="utf-8"):
if re.match(r'--', line): # ignore sql comment lines
continue
if not re.search(r';$', line): # keep appending lines that don't end in ';'
statement = statement + line
else: # when you get a line ending in ';' then exec statement and reset for next statement
statement = statement + line
#print "\n\n[DEBUG] Executing SQL statement:\n%s" % (statement)
try:
cursor.execute(statement)
except Exception as e:
print(e)
#print "\n[WARN] MySQLError during execute statement \n\tArgs: '%s'" % (str(e.args))
statement = ""
#import_tables_structure()
The problem may was that with one mysql execution there is no enough time to apply the changes in mysql.
In this answer i use a sql file parser to execution one single query at one execution.
Now the settings insertions are done with no mistake.
Related
Is it possible to view a postgresql table using Pandas?
def connectxmlDb(dbparams): conn_string = "host='{}' dbname='{}' user='{}' password='{}' port='{}'"\ .format(dbparams['HOST'], dbparams['DB'], dbparams['USERNAME'], dbparams['PASSWORD'], dbparams['PORT']) try: conn = psycopg2.connect(conn_string) pass except Exception as err: print('Connection to Database Failed : ERR : {}'.format(err)) return False print('Connection to Database Success') return conn dbconn = connectxmlDb(params['DATABASE']) The above mentioned is the code I use to connect to postgresql database sql_statement = """ select a.slno, a.clientid, a.filename, a.user1_id, b.username, a.user2_id, c.username as username2, a.uploaded_ts, a.status_id from masterdb.xmlform_joblist a left outer join masterdb.auth_user b on a.user1_id = b.id left outer join masterdb.auth_user c on a.user2_id = c.id """ cursor.execute(sql_statement) result = cursor.fetchall() This is my code to extract data from a postgres table using python. I want to know if it is possible to view this data using Pandas This is the code I used: df = pd.read_sql_query(sql_statement, dbconn) print(df.head(10)) but it is showing error. C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site- packages\pandas\io\sql.py:762: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy warnings.warn( Traceback (most recent call last): File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2023, in execute cur.execute(*args, **kwargs) psycopg2.errors.SyntaxError: syntax error at or near ";" LINE 1: ...ELECT name FROM sqlite_master WHERE type='table' AND name=?; ^ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "c:\Users\Lukmana\Desktop\user_count\usercount.py", line 55, in <module> df = pd.read_sql_table(result, dbconn) File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 286, in read_sql_table if not pandas_sql.has_table(table_name): File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2200, in has_table return len(self.execute(query, [name]).fetchall()) > 0 File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2035, in execute raise ex from exc pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': syntax error at or near ";" LINE 1: ...ELECT name FROM sqlite_master WHERE type='table' AND name=?;
How do I rectify the error in mysql-connector where I try to add multiple records at the same time into a MySQL Database?
I am trying to add multiple records into a database using mysql-connector-python. I was able to add one record initially but for multiple records, this error has been persistent. class DataBase: def __init__(self): try: self.connection = mysql.connector.connect(host='xx.xxx.xx.xx', database='XXX', user='XXXX', password='xxxx') except Error as error: print("Failed to connect: {}".format(error)) def store_into_table(self, df): mySql_insert_query = """INSERT INTO data ('data_id', 'a', 'b', 'c') VALUES ('%S','%S', '%S', '%S') """ records_to_insert = df cursor = self.connection.cursor() cursor.executemany(mySql_insert_query, records_to_insert) self.connection.commit() print(cursor.rowcount, " Records inserted successfully into data table") The variable that I am passing to an instance of DataBase is: data = [('101', 'name_1', '3', 'sample'), ('102', 'name_2', '5', 'sample_1')] Exception being thrown: Traceback (most recent call last): File "/Users/dev/anaconda3/envs/sql/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 317, in _batch_insert "Not all parameters were used in the SQL statement") ProgrammingError: Not all parameters were used in the SQL statement During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<ipython-input-123-44fdf194cd10>", line 1, in <module> db.store_into_table(data) File "/Users/dev/Detector/database.py", line 30, in store_into_table cursor.executemany(mySql_insert_query, records_to_insert) File "/Users/dev/anaconda3/envs/sql/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 350, in executemany stmt = self._batch_insert(operation, seq_params) File "/Users/dev/anaconda3/envs/sql/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 329, in _batch_insert "Failed executing the operation; %s" % err) InterfaceError: Failed executing the operation; Not all parameters were used in the SQL statement Any clues as to why this isn't working?
Try this: mySql_insert_query = """INSERT INTO data (data_id, a, b, c) VALUES (%s, %s, %s, %s) """
Python "cx_Oracle.InterfaceError: not a query" for Oracle Function return type cursor
Please help me to understand what I am doing wrong because with same value if I execute procedure which gives multiple lines of response but in python which is failing and giving exception in cursor call to function. -----Oracle Function---------- FUNCTION get_details (id IN VARCHAR2, actNo IN VARCHAR2, code IN VARCHAR2) RETURN SYS_REFCURSOR ----Oracle Function Logic---------- OPEN cur_msg_dtls FOR SELECT NAME,VALUE,CODE FROM TABLE returns cur_msg_dtls ----Python Code-------- import cx_Oracle if __name__ == '__main__': con = cx_Oracle.connect('username/pass#host.com:1521/schema') cur_var = con.cursor() actNo = 'actNo'; code ='Code'; Id ='Id'; try: cur_var.callfunc('SCHEMA.PKG.GET_DETAILS',cx_Oracle.CURSOR, (Id,actNo, actCd)) print ("hello") print(cur_var) print(type(cur_var)) for row in cur_var: print(row) except cx_Oracle.DatabaseError as e: print (e) finally: cur_var.close() con.close() pass ----Error-------------- hello <cx_Oracle.Cursor on <cx_Oracle.Connection to username/pass#host.com:1521/schema>> <class 'cx_Oracle.Cursor'> Traceback (most recent call last): File "C:\Users\TestDBPRoc.py", line 26, in <module> for row in cur_var: cx_Oracle.InterfaceError: not a query
Issue in Model-View-Controller Pattern implementation
I am running an example for learning The Model-View-Controller Pattern in python, but the code is giving an error. I tried to debug the code, but I couldn't find the main root/cause. Removing close connection system works, but what is the issue of the code? Can you advise me what is wrong? # Filename: mvc.py import sqlite3 import types class DefectModel: def getDefectList(self, component): query = '''select ID from defects where Component = '%s' ''' %component defectlist = self._dbselect(query) list = [] for row in defectlist: list.append(row[0]) return list def getSummary(self, id): query = '''select summary from defects where ID = '%d' ''' % id summary = self._dbselect(query) for row in summary: return row[0] def _dbselect(self, query): connection = sqlite3.connect('example.db') cursorObj = connection.cursor() results = cursorObj.execute(query) connection.commit() cursorObj.close() return results class DefectView: def summary(self, summary, defectid): print("#### Defect Summary for defect# %d ####\n %s" % (defectid,summary) ) def defectList(self, list, category): print("#### Defect List for %s ####\n" % category ) for defect in list: print(defect ) class Controller: def __init__(self): pass def getDefectSummary(self, defectid): model = DefectModel() view = DefectView() summary_data = model.getSummary(defectid) return view.summary(summary_data, defectid) def getDefectList(self, component): model = DefectModel() view = DefectView() defectlist_data = model.getDefectList(component) return view.defectList(defectlist_data, component) This is related run.py. #run.py import mvc controller = mvc.Controller() # Displaying Summary for defect id # 2 print(controller.getDefectSummary(2)) # Displaying defect list for 'ABC' Component print controller.getDefectList('ABC') If you need to create the database, it is available here: # Filename: datbase.py import sqlite3 import types # Create a database in RAM db = sqlite3.connect('example.db') # Get a cursor object cursor = db.cursor() cursor.execute("drop table defects") cursor.execute("CREATE TABLE defects(id INTEGER PRIMARY KEY, Component TEXT, Summary TEXT)") cursor.execute("INSERT INTO defects VALUES (1,'XYZ','File doesn‘t get deleted')") cursor.execute("INSERT INTO defects VALUES (2,'XYZ','Registry doesn‘t get created')") cursor.execute("INSERT INTO defects VALUES (3,'ABC','Wrong title gets displayed')") # Save (commit) the changes db.commit() # We can also close the connection if we are done with it. # Just be sure any changes have been committed or they will be lost. db.close() My error is as below: > Windows PowerShell Copyright (C) Microsoft Corporation. All rights > reserved. > > PS E:\Projects\test> & python e:/Projects/test/mvc.py > Traceback (most recent call last): File > "e:/Projects/test/mvc.py", line 56, in <module> > import mvc File "e:\Projects\test\mvc.py", line 65, in <module> > cursor.execute("drop table defects") sqlite3.OperationalError: no such table: defects PS E:\Projects\test> & python > e:/Projects/ramin/mvc.py Traceback (most recent call last): > File "e:/Projects/test/mvc.py", line 56, in <module> > import mvc File "e:\Projects\test\mvc.py", line 80, in <module> > print(controller.getDefectSummary(2)) File "e:\Projects\test\mvc.py", line 44, in getDefectSummary > summary_data = model.getSummary(defectid) File "e:\Projects\test\mvc.py", line 18, in getSummary > for row in summary: sqlite3.ProgrammingError: Cannot operate on a closed cursor. PS E:\Projects\test>
I suspect that the problem is this line: cursor.execute("drop table defects") Maybe you dropped that table in a previous run, and since it's no longer there, sqlite3 raises an OperationalError exception. In your code there is a comment that says that you are using an in-memory sqlite database, but you are not. This is how you create an in-memory db: db = sqlite3.connect(:memory:) If you use an in-memory db you don't need to drop anything, since you are creating the db on the fly when you run your script. Note: last year I wanted to understand the MVC better, so I wrote a series of articles about it. Here is the one where I use SQLite as a storage backend for my Model.
Problem Using Variable in Sqlite3 Statement
I have a problem with a Python 2.7 project. I'm trying to set a variable to a value retrieved from an sqlite3 database, but I'm having trouble. Here is my code thus far, and the error I'm receiving. Yes, the connection opens just fine, and the table, columns, and indicated row are there as they should be. import sqlite3 import Trailcrest conn = sqlite3.connect('roster.paw') c = conn.cursor() def Lantern(AI): """Pulls all of the data for the selected user.""" Trailcrest.FireAutoHelp = c.execute("""select fireautohelp from roster where index = ?;""", (AI,) ).fetchall() The error is: > Traceback (most recent call last): > File "<pyshell#4>", line 1, in <module> Lantern(1) > File "C:\Users\user\MousePaw Games\Word4Word\PYM\Glyph.py", line 20, > in Lantern > Trailcrest.FireAutoHelp = c.execute("""select fireautohelp from roster where index = ?;""", (AI,)).fetchall() > OperationalError: near "index": syntax error
As Thomas K mentions in a comment, index is a SQL keyword. You can either rename that column, or enclose in backticks: Trailcrest.FireAutoHelp = c.execute("""select fireautohelp from roster where `index` = ?;""", (AI,) ).fetchall()