S
S
Sergey Yelpashev2019-06-18 22:00:34
Python
Sergey Yelpashev, 2019-06-18 22:00:34

Why doesn't INSERT INTO work with pymysql?

It is not possible to execute a simple SQL command using the pymysql module, but for example, SELECT works.
Here is the main script:

from connect import get_connection
import pymysql

connection = get_connection()
c = connection.cursor()
sql = "INSERT INTO test (id, name) VALUES (10, 'test')"
c.execute(sql)
connection.close()

Here is the connect:
import pymysql.cursors  
# Подключиться к базе данных.
def get_connection():
  connection=pymysql.connect(host='localhost',
                             user='root',
                             password='', 
                             db='test',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
  print ("Успешное подключение к базе данных!\n\n\n")
  connection.commit()
  return connection

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2019-06-18
@MrSedan

I don't know what it is PyMySQL, but the network gives advice :

connection = get_connection()
c = connection.cursor()
sql = "INSERT INTO test (id, name) VALUES (10, 'test')"
c.execute(sql)
connection.commit()
connection.close()

S
Stalker_RED, 2019-06-18
@Stalker_RED

Perhaps there already is a record with id=10
Doesn't give any errors?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question