如何使用Python将数据插入本地数据库? [英] How do I insert data into local database with Python?

查看:242
本文介绍了如何使用Python将数据插入本地数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据插入我的本地数据库,但它一直向我显示错误(虽然已建立连接):



Traceback(最近一次调用最后一次) :

文件/Users/mahmoudtarek/Desktop/mth1/index.py,第60行,在add_mahmoud_friends中

self.c.execute('''INSERT INTO mtth (mth_friends)VALUE(%s)''',(mth_friends))

文件/Users/mahmoudtarek/PycharmProjects/untitled/venv/lib/python3.7/site-packages/mysql/connector /cursor.py,第533行,执行

如果不是self._connection:

ReferenceError:弱引用的对象不再存在

处理完成后退出代码134(被信号6中断:SIGABRT)









所以问题出在哪里?



我尝试过:



I tried to insert data into my local database but it keeps showing me error like that(although connection is established) :

Traceback (most recent call last):
File "/Users/mahmoudtarek/Desktop/mth1/index.py", line 60, in add_mahmoud_friends
self.c.execute('''INSERT INTO mtth (mth_friends) VALUE (%s)''',(mth_friends))
File "/Users/mahmoudtarek/PycharmProjects/untitled/venv/lib/python3.7/site-packages/mysql/connector/cursor.py", line 533, in execute
if not self._connection:
ReferenceError: weakly-referenced object no longer exists
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)




so, where is the problem ?

What I have tried:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import mysql.connector
import mysql.connector.cursor
from mysql.connector import errorcode
import sys
import os
from os import path
from PyQt5.uic import loadUiType

FORM_CLASS,_ = loadUiType(path.join(path.dirname(__file__),"mainwindow.ui"))

class Main(QMainWindow,FORM_CLASS):
    def __init__(self,parent=None):
        super(Main,self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.InitUI()
        self.Handel_buttons()
        self.Handel_db_connections()

    def InitUI(self):
        ## changes in the run time
        pass

    def Handel_buttons(self):
          ## all buttons in the app
        self.pushButton.clicked.connect(self.add_mahmoud_friends)
        self.pushButton_3.clicked.connect(self.update_mahmoud_friends)
        self.pushButton_2.clicked.connect(self.delete_mahmoud_friends)

    def Handel_db_connections(self):

        try:
            conn = mysql.connector.connect(host='127.0.0.1',
                                                       database='mydb',
                                                       user='root',
                                                       password='************',
                                                       use_pure=True)  # use_pure is set to true
            if conn.is_connected():
                db_Info = conn.get_server_info()
                print("Connected to MySQL database using C extension... MySQL Server version on ", db_Info)
                self.c = conn.cursor()

        except errorcode as e:
            print("Error while connecting to MySQL using C extension", e)
        finally:
            # closing database connection.
            if (conn.is_connected()):


                print("connection is closed")
    ####################################################
    ## mahmoud info


    def add_mahmoud_friends(self):
        mth_friends = self.lineEdit.text()
        self.c.execute('''INSERT INTO mtth (mth_friends) VALUE (%s)''',(mth_friends))

        print("done")

    def update_mahmoud_friends(self):
        pass

    def delete_mahmoud_friends(self):
        pass

def main():
    app= QApplication(sys.argv)
    window =Main()
    window.show()
    app.exec_()

if __name__ == '__main__':
         main()

推荐答案

self.c.execute('''INSERT INTO mtth (mth_friends) VALUE (%s)''',(mth_friends))





尝试改为:



try instead:

self.c.execute('''INSERT INTO mtth (mth_friends) values (%s)''',(mth_friends))





请注意,VALUE这个词是p带有s:值的壁画



Notice that the word VALUE is plural with an s : values


这篇关于如何使用Python将数据插入本地数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆