Python mysql build table error - python
#-*-coding:utf-8-*-
import pymysql
class get_Mysql(object):
def __init__(self,dbname,company,goods_name):
self.dbname = dbname
self.table_name = '{}_{}'.format(company,goods_name)
self.conn = pymysql.connect(
host = '127.0.0.1',
user = 'root',
password = '123456',
port = 3306,
db = self.dbname,
charset = 'utf8'
)
self.cursor = self.conn.cursor()
def create_table(self):
create_sql = ''' CREATE TABLE '{tbname}' (
{id} INT (15) PRIMARY KEY,
{price} VARCHAR (15) NOT NULL,
{is_jd} CHAR (30) NOT NULL DEFAULT NULL,
{shopname} VARCHAR (30) NOT NULL DEFAULT NULL,
{brand} VARCHAR (20) NOT NULL DEFAULT NULL,
{years} VARCHAR (10) NOT NULL DEFAULT NULL,
{months} VARCHAR (10) NOT NULL DEFAULT NULL,
{weight} VARCHAR (10) NOT NULL DEFAULT NULL,
{thick} VARCHAR (40) NOT NULL DEFAULT NULL,
{long} VARCHAR (40) NOT NULL DEFAULT NULL,
{cpu_brand} VARCHAR (30) NOT NULL DEFAULT NULL,
{cpu_num} VARCHAR (20) NOT NULL DEFAULT NULL,
{sim_num} VARCHAR (25) NOT NULL DEFAULT NULL,
{sim} VARCHAR (20) NOT NULL DEFAULT NULL,
{rom} VARCHAR (10) NOT NULL DEFAULT NULL,
{ram} VARCHAR (15) NOT NULL DEFAULT NULL,
{sizes} VARCHAR (20) NOT NULL DEFAULT NULL,
{front_c} VARCHAR (20) NOT NULL DEFAULT NULL,
{back_c} VARCHAR (20) NOT NULL DEFAULT NULL,
{battery} VARCHAR (45) NOT NULL DEFAULT NULL,
{total_com} INT (20) NOT NULL DEFAULT 0,
{good_com} INT (20) NOT NULL DEFAULT 0,
{mid_com} INT (20) NOT NULL DEFAULT 0,
{bad_com} INT (20) NOT NULL DEFAULT 0,
{good_lv} FLOAT (20),
{mid_lv} FLOAT (20),
{bad_lv} FLOAT (20)
)
'''
try:
self.cursor.execute(create_sql.format(tbname=self.table_name,id='id',price='price',is_jd='is_jd',shopname='shopname',brand='brand',
years='years',months='months',weight='weight',thick='thick',long='long',cpu_brand='cpu_brand',
cpu_num='cpu_num',sim_num='sim_num',sim='sim',rom='rom',ram='ram',sizes='sizes',
front_c='front_c',back_c='back_c',battery='battery',total_com='total_com',good_com='good_com',
mid_com='mid_com',bad_com='bad_com',good_lv='good_lv',mid_lv='mid_lv',bad_lv='bad_lv'))
except Exception as e:
self.conn.rollback()
print('Create table failure, cause:',e)
else:
self.conn.commit()
print('The table is successful and the name is{}'.format(self.table_name))
def insert(self,data):
insert_sql = '''INSERT INTO '{tbname}'(id,price,is_jd,shopname,brand,years,months,weight,thick,long,cpu_brand,cpu_num,sim_num,sim,
rom,ram,sizes,front_c,back_c,battery,total_com,good_com.mid_com,bad_com,good_lv,mid_lv,bad_lv)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
try:
self.cursor.execute(insert_sql.format(self.table_name),data['id'],data['price'],data['is_jd'],data['shopname'],data['brand'],data['years'],data['months'],data['weight'],data['thick'],
data['long'],data['cpu_brand'],data['cpu_num'],data['sim_num'],data['sim'],data['rom'],data['ram'],data['sizes'],data['front_c'],
data['back_c'],data['battery'],data['total_com'],data['good_com'],data['mid_com'],data['bad_com'],data['good_lv'],data['mid_lv'],data['bad_lv'])
except Exception as e:
self.conn.rollback()
print("Insert data failure, cause:",e)
else:
self.conn.commit()
print('Insert a data successfully!')
def close_table(self):
self.cursor.close()
self.conn.close()
if __name__ == '__main__':
data ={'id':13108530411,'price':'2900.00','is_jd':'self-operation','shopname':'Chili mobile phone flagship store',"brand":'Hot pepper','years':'2017','months':'June','weight':'164(Contain the battery)',"thick":"8.9(Note: subject to the product configuration and manufacturing process, the actual size of the body is different. Please refer to the physical object)",
'long':'145(Note: subject to the product configuration and manufacturing process, the actual size of the body is different. Please refer to the physical object)','cpu_brand':'Snapdragon','cpu_num':'Four nuclear','sim_num':'Double card double for single pass','sim':'Nano SIM','rom':'32GB','ram':'4GB',"sizes":'5.0 inches','front_c':'8 million pixels','back_c':'16 million pixels','battery':'4000mAh (Typical capacity)/3900mAh (Nominal capacity','total_com':1400,
'good_com':1300,'mid_com':60,'bad_com':40,'good_lv':0.925,'mid_lv':0.043,'bad_lv':0.032}
my = get_Mysql('e-commerce','JD','phone')
my.create_table()
my.insert(data)
my.close_table()
I don't use markdown very much, my code indent is not a problem, I pasted code to stackoverflow some indentation problems, please ignore.
I made a mistake when I created the data table using pymysql:
Create table failure, cause: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''JD_phone' (\n id INT (15) PRIMARY KEY,\n price VARCHAR (15) NOT NUL' at line 1")
Insert data failure, cause: 'tbname'
So is my SQL statement wrong, and how do I write the remaining decimal point in the SQL statement?
Oh, I've solved it, by testing every field test,I changed the long field to longs to successfully create the data table, which should be the reason for the built-in name conflict with mysql.
The table is successful and the name is JD_phone
Related
incorrect integer value mySQL
Im receiving an error where I am using an incorrect integer value for userID_fk and target. The error comes up for values which have an integer as their data type and if its changed to text or varchar it will state a site has been created and the siteID will increase but no other data will be included. I want the user to input their username so its matched with its userID and inserted into userID_fk through python with Tkinter. Below is the structure for my users and sites table users: CREATE TABLE `users` ( `userID` int(255) NOT NULL AUTO_INCREMENT, `userName` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL, `userPassword` varchar(225) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL, `Name` varchar(255) NOT NULL, `phoneNum` text NOT NULL, `email` varchar(230) NOT NULL, `region` text NOT NULL, `accessLevel` int(10) NOT NULL, PRIMARY KEY (`userID`) ) ENGINE=InnoDB AUTO_INCREMENT=10002 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT sites: CREATE TABLE `sites` ( `siteID` int(225) NOT NULL AUTO_INCREMENT, `siteName` text CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL, `userID_fk` int(255) NOT NULL, `region` text NOT NULL, `risklevel` text NOT NULL, `siteType` text NOT NULL, `target` int(225) NOT NULL, PRIMARY KEY (`siteID`), KEY `userID_fk` (`userID_fk`), CONSTRAINT `sites_ibfk_1` FOREIGN KEY (`userID_fk`) REFERENCES `users` (`userID`) ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT Python code to insert a site into the sites table: def register_site(): sitename_info = sitename2.get() username2_info = username2.get() region_info = region.get() risklevel_info = risklevel.get() sitetype_info = sitetype.get() targetpercent_info = targetpercent.get() # Sql code for writing the data that was written in the regsitering page. cursor = cnn.cursor() sitequery = "INSERT INTO `sites`(`siteID`, `siteName`, `userID_fk`, `region`, `risklevel`, `siteType`, `target`) VALUES (NULL,%s,%s,%s,%s,%s,%s)" sitequery_vals = (sitename_info, username2_info, region_info, risklevel_info, sitetype_info, targetpercent_info) cursor.execute(sitequery, sitequery_vals) cnn.commit() cursor.close() cnn.close() # removes the values in the entrys once the user selects that the registration was successful sitename2_entry.delete(0, END) region_entry.delete(0, END) risklevel_entry.delete(0, END) sitetype_entry.delete(0, END) targetpercent_entry.delete(0, END) Label(screen10, text = "Site Created", fg = "green", font = ("calibri", 11)).pack()
If username2_info is the userName, you need to get the userID from the users table: sitequery = ("INSERT INTO `sites` (`siteName`, `userID_fk`, `region`, `risklevel`, `siteType`, `target`) " "SELECT %s, `userID`, %s, %s, %s, %s FROM `users` WHERE `userName` = %s") sitequery_vals = (sitename_info, region_info, risklevel_info, sitetype_info, targetpercent_info, username2_info) cursor.execute(sitequery, sitequery_vals) cnn.commit()
MySQL prepared statements causing SQL syntax error
I am using prepared statements for my SQL insert query, and I am receiving the message that there is an error in the syntax. I have tried using PHPMyAdmin and used the same query in that and substituted the placeholders for the real values and that query worked fine, therefore I am assuming it is something to do with my use of prepared statements. def create_user(f_name, s_name, email, sex, dob, mobile=None): try: conn = mysql.connector.connect(host=host, user=user, passwd=password) # create a connection to the database cursor = conn.cursor(prepared=True) # Creates a cursor that is expecting prepared if mobile is not None: # if the mobile number is specified sql_parameterized_query = ("""BEGIN; INSERT INTO users (FirstName, Surname, Email, Dob, Gender, Mobile) VALUES (%s, %s, %s, %s, %s, %s); INSERT INTO passwords (UserID, hashedPass) VALUES (LAST_INSERT_ID(),%s); COMMIT;""") query_array = (f_name, s_name, email, date_sql_format(dob), sex, mobile, hash_user_password) else: # if the mobile number is not specified sql_parameterized_query = ("""BEGIN; INSERT INTO users (FirstName, Surname, Email, Dob, Gender) VALUES(%s, %s, %s, %s, %s); INSERT INTO passwords (UserID, hashedPass) VALUES(LAST_INSERT_ID(),%s); COMMIT;""") query_array = (f_name, s_name, email, date_sql_format(dob), sex, hash_user_password) # Init array of values cursor.execute(sql_parameterized_query, query_array) # Execute query conn.commit() I would like it to insert the details for a new user into the database all fields are required excluding the mobile phone number, that is why I have used the if statement to separate them, if this is poor practice then please guide me in the correct direction for that too as I could not find a more elegant way of solving that issue, anyway, when calling the function like so create_user("Ollie", "Pugh", "oliver.pugh#icloud.com", "M", "22-04-2001") The variable query_array has the value of ('Ollie', 'Pugh', 'oliver.pugh#icloud.com', '2001-04-22', 'M', '$2b$12$RU9FRcNjgHlC78kjZA5OIeqT1s1K2LHndC2iDK8mcqkadGc8d9XO2') The message I receive is: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO users (FirstName, Surname, Email, Dob, Gender)' at line 2 The structure of the table Users is: CREATE TABLE `users` ( `UserID` int(11) NOT NULL AUTO_INCREMENT, `FirstName` varchar(255) NOT NULL, `Surname` varchar(255) NOT NULL, `Email` varchar(255) NOT NULL, `Dob` date NOT NULL, `Gender` char(1) NOT NULL, `Mobile` varchar(11) DEFAULT NULL, `timeOfCreation` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `Authority` varchar(255) NOT NULL DEFAULT 'User', PRIMARY KEY (`UserID`) ) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1
The solution to my problem was to create a procedure, I named it CreateUser and consisted of: BEGIN START TRANSACTION; IF mobile = "NULL" THEN SET mobile = null; END IF; INSERT INTO `cl43-flexfit`.users (FirstName, Surname, Email, Dob, Gender,Mobile) VALUES (f_name, s_name, email, dob, gender, mobile); INSERT INTO `cl43-flexfit`.passwords (UserID, hashedPass) VALUES (LAST_INSERT_ID(), passhash); COMMIT; END And I have modified the python script to have two cursors as I could not use a USE statement within the procedure nor could I use one with a cursor that was configured for prepared statements. try: conn = mysql.connector.connect(host=host, user=user, passwd=password) # create a connection to the database cursor = conn.cursor() # Have to have two as you can not select database with a prepared cursor prep_cursor = conn.cursor(prepared=True) # Creates a cursor that is expecting prepared if mobile is None: # if the mobile number is not specified mobile = "NULL" # This is recognised as null in the procedure sql_parameterized_query = "CALL CreateUser(%s, %s, %s, %s, %s, %s, %s)" # Calls the create user procedure query_array = (f_name, s_name, email, date_sql_format(dob), sex, mobile, hash_user_password) cursor.execute("USE `cl43-flexfit`;") prep_cursor.execute(sql_parameterized_query, query_array) # Execute query conn.commit() I'm sure there are still better ways of doing this, but this does the job for now.
How to leave the unnecessary quotation marks when i convert a csv into a mysqldb with python?
I've got a CSV excel file, which one I want to convert into mysqldb. It is working very fine, but in every cell in the MYSQLdb there are unnecessary quotation marks, like this: "". When the cell is empty I see this: "" When the cell is not empty is see this: "somethingdata" I can't understand, why put these quotation marks when in the csv file there are none. Here my code, I think it is correct too. connection = MySQLdb.connect(host='localhost', user='root', passwd='1234', db='database') cursor = connection.cursor() query = """ CREATE TABLE `test` ( `Megnevezes` varchar(100) DEFAULT NULL, `2015` varchar(100) DEFAULT NULL, `2014` varchar(100) DEFAULT NULL, `2013` varchar(100) DEFAULT NULL, `2012` varchar(100) DEFAULT NULL, `2011` varchar(100) DEFAULT NULL, `ID` int(10) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`ID`) ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8""" cursor.execute(query) connection.commit() cursor.close() connection = MySQLdb.connect(host='localhost', user='root', passwd='1234', db='database') cursor = connection.cursor() query = """ load data local infile 'C:/Python27/output.csv' into table test character set latin1 fields terminated by ';' lines terminated by '\n' ignore 1 lines; """ cursor.execute(query) connection.commit() cursor.close() Any ideas how can I fix this issue?
https://dev.mysql.com/doc/refman/5.7/en/load-data.html Use the: ENCLOSED BY 'char' where char will be "
How to commit MySql table name as variable in python?
I want to create a new mysqldb table for each unique user, but i'm getting errors: 1.'bytes' object has no attribute 'encode' 2.Can't convert 'bytes' object to str implicitly 3.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Text'( id int(11) NOT NULL AUTO_INCREMENT, UserName text NOT NULL' at line 1 c.execute("CREATE TABLE IF NOT EXISTS {table_name}".format(table_name=belekas), (belekas) + """( `id` int(11) NOT NULL AUTO_INCREMENT, `UserName` text NOT NULL, `Data` date NOT NULL, `Laikas` time NOT NULL, `KeyStrokes` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8""") con.commit() c.execute("INSERT INTO {table_name} VALUES (id, %s, Data, Laikas, %s)".format(table_name=belekas), (belekas, vartotojas, tekstas)) con.commit() I tried using: c.execute("CREATE TABLE IF NOT EXISTS" + vartotojas + """( and this: c.execute("CREATE TABLE IF NOT EXISTS" + repr(vartotojas.decode('utf-8')) + """( and this: c.execute("CREATE TABLE IF NOT EXISTS {this_table}".format(this_table=vartotojas), (vartotojas.encode("utf-8")) + """( Can someone suggest solution for this problem?
Using python script downloading the data into oracle data base
By using python I am downloading the data into a MySQL database, but I want to change it Oracle. Can anyone help with this> For the Oracle database I have username, password, hostname, port, and SID. Are these details are enough to change the database? <` db = MySQLdb.connect("127.0.0.1", "root", "1234") cursor = db.cursor() try: cursor.execute('create database nasa') except: None cursor.execute('use nasa') cursor.execute( "CREATE TABLE IF NOT EXISTS temperatureTable(date VARCHAR(30) NOT NULL, time VARCHAR(30) NOT NULL, longitude VARCHAR(30) NOT NULL, latitude VARCHAR(30) NOT NULL, temperature VARCHAR(30) NOT NULL, PRIMARY KEY (date, longitude, latitude))") lineToBeStored = 0 for lat in numpy.nditer(latitude): for lon in numpy.nditer(longitude): cursor.execute( "INSERT INTO temperatureTable VALUES('%s', '%s', '%s', '%s', '%s')" % ( today, dateTime, lat, lon, temperatureData[lineToBeStored]))`>