2017-08-29 1 views
-1

Ich versuche, eine Datenbank, die sich auf einem Webserver befindet, mit einem Roboter zu verbinden, aber ich weiß nicht, wie ich die Datenbank mit dem Roboter verbinden soll. Ich möchte, dass der Roboter SELECT- und UPDATE-Abfragen vom Roboter ausführt. Das andere Problem ist, dass ich nicht vorhabe, C-Sprachen oder Java zu verwenden; Ich plane Python im Hauptkontrollsystem zu verwenden.Wie würde man eine SQL-Datenbank sicher mit einem externen Client verbinden?

Ich weiß: PHP VBScript Batch Python

Wenn jemand weiß, wie die DB zu einem Bot zu verbinden wäre es eine große Hilfe sein.

Antwort

0

Also im Grunde, wie Sie eine Verbindung zu einer SQL DB in Python herstellen? Ich arbeite gerade an einem virtuellen Bot und mache dasselbe. Schau in das Modul, SQL-Connector!
http://www.mysqltutorial.org/python-connecting-mysql-databases/
Sie würden beginnen mit einer config.ini mit Ihren Anmeldeinformationen zu schaffen

[mysql] 
host = localhost 
database = python_mysql 
user = root 
password = 

lesen Config.ini und zurück ein Wörterbuch

from configparser import ConfigParser 
def read_db_config(filename='config.ini', section='mysql'): 
    """ Read database configuration file and return a dictionary object 
    :param filename: name of the configuration file 
    :param section: section of database configuration 
    :return: a dictionary of database parameters 
    """ 
    # create parser and read ini configuration file 
    parser = ConfigParser() 
    parser.read(filename) 

    # get section, default to mysql 
    db = {} 
    if parser.has_section(section): 
     items = parser.items(section) 
     for item in items: 
      db[item[0]] = item[1] 
    else: 
     raise Exception('{0} not found in the {1} file'.format(section, filename)) 

    return db 

und eine Verbindung zum MySQL-Datenbank

from mysql.connector import MySQLConnection, Error 
from python_mysql_dbconfig import read_db_config 


def connect(): 
    """ Connect to MySQL database """ 

    db_config = read_db_config() 

    try: 
     print('Connecting to MySQL database...') 
     conn = MySQLConnection(**db_config) 

     if conn.is_connected(): 
      print('connection established.') 
     else: 
      print('connection failed.') 

    except Error as error: 
     print(error) 

    finally: 
     conn.close() 
     print('Connection closed.') 


if __name__ == '__main__': 
    connect() 

und aktualisieren Aussage würde aussehen wie die folgende

def update_book(book_id, title): 
    # read database configuration 
    db_config = read_db_config() 

    # prepare query and data 
    query = """ UPDATE books 
       SET title = %s 
       WHERE id = %s """ 

    data = (title, book_id) 

    try: 
     conn = MySQLConnection(**db_config) 

     # update book title 
     cursor = conn.cursor() 
     cursor.execute(query, data) 

     # accept the changes 
     conn.commit() 

    except Error as error: 
     print(error) 

    finally: 
     cursor.close() 
     conn.close() 


if __name__ == '__main__': 
    update_book(37, 'The Giant on the Hill *** TEST ***') 
+0

Ist das, was Sie suchen? – Pacified

Verwandte Themen