Answer the question
In order to leave comments, you need to log in
Why is mysql server always running?
To connect mysql to python I use mysql-connector-python library.
As I noted, for it to work, I must first enable the mysql server, but I'm going to upload my script to the hosting (heroku), so I want to clarify how I can make the server work all the time or maybe other working solutions.
My code:
import mysql.connector
from mysql.connector import Error
def create_connection_mysql_db(db_host, user_name, user_password, db_name = None):
connection_db = None
try:
connection_db = mysql.connector.connect(
host = db_host,
user = user_name,
passwd = user_password,
database = db_name
)
print("Подключение к MySQL успешно выполнено")
except Error as db_connection_error:
print("Возникла ошибка: ", db_connection_error)
return connection_db
try:
conn = create_connection_mysql_db('localhost',
'root',
'pass')
cursor = conn.cursor()
create_db_sql_query = 'CREATE DATABASE IF NOT EXISTS {}'.format('raffle')
cursor.execute(create_db_sql_query)
cursor.close()
conn.close()
conn = create_connection_mysql_db('localhost',
'root',
'pass',
"raffle")
cursor = conn.cursor()
create_table_query = '''
CREATE TABLE IF NOT EXISTS links (
id INT AUTO_INCREMENT,
link TEXT NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB'''
cursor.execute(create_table_query)
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
def start:
#code
#code
#code
try:
conn = create_connection_mysql_db('localhost',
'root',
'pass',
"raffle")
cursor = conn.cursor()
delete_Usa_users_query = '''
DELETE FROM users;
'''
cursor.execute(delete_Usa_users_query)
conn.commit()
ll=links.split(', ')
for lean in ll:
insert_users_table_query = f'''
INSERT INTO
`users` (`link')
VALUES
('{lean}');'''
cursor.execute(insert_users_table_query)
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
Answer the question
In order to leave comments, you need to log in
create_connection_mysql_db('localhost',
why localhost? Many have already said: heroku resets local databases. So it's better to use cloud databases, in your case pymongo
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question