Working with Python databases without ORM

What is the standard / recommended way to work with a database (mainly MySQL) in Python?

I do not want ORM, and the easier, the better. I do not mind writing my own SELECT, but I would like something that displays a dictionary, for example, for INSERT and UPDATE. I'm basically a PHP programmer, and I'm used to doing something like this:


$data = array(
  'foo' => 'bar'
);

$insert = $db->insert('table', $data);

Is there something similar for Python? I looked into SQLAlchemy, which apparently uses everything, and its “SQL expression” seems nice, but it seems like overusing it all to write raw SQL.

+3
source share
2 answers

dict , DB, "named" (placeholder) paramstyle ( , "paramstyle" ):

sqlite3 docs:

import sqlite3
con = sqlite3.connect("mydb")    
cur = con.cursor()

who = "Yeltsin"
age = 72

cur.execute("select name_last, age from people where name_last=:who and age=:age",
    {"who": who, "age": age})
print cur.fetchone()

, MySQL : MySQLdb oursql "named" paramstyle. MySQLdb "format" paramstyle ( "% s" ), oursql "qmark" ( "?" ). list dict :

x={'bar':1.0,'baz':2.0}
sql='INSERT INTO foo (bar,baz) VALUES (?,?)'
cur.execute(sql,[x['bar'],x['baz']])

, MySQL, oursql. MySQLdb Python-MySQL, regex SQL , MySQL. , oursql , SQL , . oursql - , MySQLdb.

+5

, , , . SQLite . mySQL mySQLdb. .

0

Source: https://habr.com/ru/post/1790701/


All Articles