与psycopg2 postgresq连接的python文件

2024-09-30 06:22:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个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()

但我想把它分成两个文件 康涅狄格州零件连接和 插入.py在postgreql表中插入数据 像这样的人谁知道php

你知道吗康奈克斯.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();
}

你知道吗插入.php你知道吗

require_once("connex.php");
$sql = "INSERT INTO trens ( `nom`, `prenom`)VALUES ('RASOLO','Solofo')";
$conn->exec($sql );

Tags: localhosthostsqlexceptionerrorconnnompsycopg2
1条回答
网友
1楼 · 发布于 2024-09-30 06:22:51

文件: 康奈克斯公司你知道吗

import psycopg2
myConnection = psycopg2.connect(host="localhost", port=5432, user="postgres", 
password="mypass", dbname="dell")
conn = myConnection.cursor()

文件呢 插入.py(导入de connex)

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)

相关问题 更多 >

    热门问题