pip3 install --user mysql-connector
import mysql.connector conn = mysql.connector.connect( host="localhost", user="penztar", passwd="titok" ) print(conn)
Vagy:
import mysql.connector config = { 'host': 'localhost', 'user': 'feherbt', 'passwd':'titok' } conn = mysql.connector.connect(**config, database='feherbt') print(conn)
A kimenet ehhez hasonló kell legyen:
<mysql.connector.connection.MySQLConnection object at 0x7f01954f6d30>
import mysql.connector conn = mysql.connector.connect( host="localhost", user="penztar", passwd="titok" ) cur = conn.cursor() cur.execute('create database penztar')
import mysql.connector conn = mysql.connector.connect( host="localhost", user="feherbt", passwd="titok", database='feherbt' ) cur = conn.cursor() sql = 'select * from employees' cur.execute(sql) conn.close() print(cur.fetchall())
import mysql.connector conn = mysql.connector.connect( host="localhost", user="feherbt", passwd="titok", database='feherbt' ) cur = conn.cursor() sql = 'select * from employees' cur.execute(sql) conn.close() for (id, name, city, salary) in cur: print(f"{id}:{name}:{city}:{salary}")
import mysql.connector config = { 'host': 'localhost', 'user': 'feherbt', 'passwd':'titok' } conn = mysql.connector.connect(**config, database='feherbt') cur = conn.cursor() sql = """ insert into employees (name, city, salary) values ('Tekerős Ferenc', 'Hatvan', 2877000) """ cur.execute(sql) conn.commit() conn.close() print('Utoljára beszúrt: ', cur.lastrowid) print('Érintett rekordok száma: ', cur.rowcount)
import mysql.connector config = { 'host': 'localhost', 'user': 'feherbt', 'passwd':'titok' } conn = mysql.connector.connect(**config, database='feherbt') cur = conn.cursor() sql = """ insert into employees (name, city, salary) values (%s, %s, %s) """ values = ("Perec Dénes", "Szeged", 2800400) cur.execute(sql, values) conn.commit() conn.close() print('Utoljára beszúrt: ', cur.lastrowid) print('Érintett rekordok száma: ', cur.rowcount)
import mysql.connector config = { 'host': 'localhost', 'user': 'feherbt', 'passwd':'titok' } conn = mysql.connector.connect(**config, database='feherbt') cur = conn.cursor() sql = """ insert into employees (name, city, salary) values (%s, %s, %s) """ values = [ ("Filip Dezső", "Szeged", 3500000), ("Andri Árpád", "Szeged", 3500000), ("Csikos Lóránt", "Szeged", 3500000), ] cur.executemany(sql, values) conn.commit() conn.close() print('Három közül az elsőként beszúrt: ', cur.lastrowid) print('Érintett rekordok száma: ', cur.rowcount)