我是一名PHP开发人员。我正在尝试使用pyqt创建一个小应用程序 我使用的代码效果很好
import psycopg2
try:
myConnection = psycopg2.connect(host="localhost", user="openpg", password="", dbname="dbLaSolve")
conn = myConnection.cursor()
except(Exception, psycopg2.Error) as error :
if(myConnection):
print("Failed to insert record", error)
sql = "insert into trens(nom,prenom)VALUES('RASOLO','Solofo')"
conn.execute(sql)
myConnection.commit()
但是我想将其分为两个文件示例 conn.py 用于零件连接, insert.py 将数据插入表postgreql 对于那些了解php的人来说像这样
connex.php
try
{
$conn = new PDO('mysql:host=localhost;dbname=dbLaSolve','root','');
}
catch(Exception $err)
{
echo "Erreur de connexion à la base de donnée: ".$err->getMessage();
exit();
}
insert.php
require_once("connex.php");
$sql = "INSERT INTO trens ( `nom`, `prenom`)VALUES ('RASOLO','Solofo')";
$conn->exec($sql );
答案 0 :(得分:0)
文件: connex.py
import psycopg2
myConnection = psycopg2.connect(host="localhost", port=5432, user="postgres",
password="mypass", dbname="dell")
conn = myConnection.cursor()
和文件 insert.py(导入连接)
from connect import myConnection,conn,psycopg2
try:
sql = "insert into trens VALUES('hola','mundo')"
conn.execute(sql)
myConnection.commit()
except(Exception, psycopg2.Error) as error :
if(myConnection):
print("Failed to insert record", error)