2013-08-27 17 views
3

我使用MySQLdb操縱MySQL數據庫,我有這個按照常規,注入在一個表中的一些數據稱爲urls數據插入到MySQL,但不是永久 - Python的

def insert_urls(dbconn, filenames): 
    root = "<path>/" 
    link = "http://<url>/" 
    for f in filenames: 
     filename = root + f + ".html" 
     local_url = link + f + ".html" 
     print(filename, local_url) 
     sql = """ 
     INSERT INTO urls(url, filename) VALUES('%s', '%s'); 
     """ % (local_url, filename) 
     print(sql) 
     dbconn.execute_query(sql) 

的所述urls表的聲明這裏找到:

def create_urls_table(): 

    sql = """ 
     CREATE TABLE IF NOT EXISTS urls (
      id INT NOT NULL AUTO_INCREMENT, 
      url BLOB NOT NULL, 
      filename BLOB NOT NULL, 
      PRIMARY KEY(id) 
     ) ENGINE=INNODB; 
    """ 
    return sql 

dbconn是類Dbconn的對象,定義爲:

class Dbconn: 
    def __init__(self, 
       host="", 
       user="", 
       pwd="", 
       database=""): 

     self.host = host 
     self.user = user 
     self.pwd = pwd 
     self.db = database 
     self.cursor = None 
     self.conn = None 

     try: 
      self.conn = MySQLdb.connect(host=self.host, 
             user=self.user, 
             passwd =self.pwd, 
             db=self.db) 
      self.cursor = self.conn.cursor() 
      print "Connection established" 
     except MySQLdb.Error, e: 
      print "An error has occurred ", e 

    def execute_query(self, sql=""): 
     try: 
      self.cursor.execute(sql) 
     except MySQLdb.Error, e: 
      print "An error has occurred ", e 

運行insert_urls我得到以下輸出的步驟之後:

INSERT INTO urls(url, filename) VALUES ('http://<url>/amazon.html','<path>/amazon.html'); 
    INSERT INTO urls(url, filename) VALUES('http://<url>/linkedin.html', '<path>/linkedin.html'); 
    INSERT INTO urls(url, filename) VALUES('http://<url>/nytimes.html', '<path>/nytimes.html'); 

其中我能夠通過命令行手動注入MySQL。 但是做一個SELECT * FROM urls查詢我什麼也沒找到。我手動插入兩行後,我得到了:

mysql> select * from urls; 
+----+------------------------------------------------+------------------------+ 
| id | url           | filename    | 
+----+------------------------------------------------+------------------------+ 
| 19 | http://<url>/yelp.html       | <path>/yelp.html  |  
| 29 | http://<url>/amazon.html      | <path>/amazon.html  | 
+----+------------------------------------------------+------------------------+ 

請注意id值遞增......它可能表示有數據插入,但不持久化?有人可以幫助我嗎? (事實上​​,INNODB是一個事務型數據庫)

回答

4

您可能正在使用一個交易數據庫,在這種情況下,你必須調用

self.conn.commit() 


您可以納入commitexecute_query

def execute_query(self, sql=""): 
    try: 
     self.cursor.execute(sql) 
    except MySQLdb.Error as e: 
     print "An error has occurred ", e 
     self.conn.rollback() 
    else: 
     self.conn.commit() 

然而,有c應該在您致電commitrollback之前希望執行多個查詢的情況下。在這種情況下,您希望從execute_query中刪除commit,並在需要時明確呼叫commit,或者在退出with -suite時使用上下文管理器調用commit


請注意,MySQLdb連接是上下文管理器。你可以寫

connection = MySQLdb.connect(
    host=config.HOST, user=config.USER, 
    passwd=config.PASS, db=config.MYDB,) 

with connection as cursor: 
    cursor.execute(...) 

,連接將調用connection.commit()在退出with-suite,或connection.rollback()如果有一個例外。 下面是在MySQLdb.connections.py控制此代碼:

def __enter__(self): return self.cursor() 

def __exit__(self, exc, value, tb): 
    if exc: 
     self.rollback() 
    else: 
     self.commit() 
+0

上的錢!THXü – cybertextron

1

您​​語句後,調用commit()

self.cursor.execute(sql) 
self.conn.commit() 

欲瞭解更多信息,請參見:python mysql insert data

+0

回溯(最近通話最後一個): 文件 「download_page.py」,線路122,在 的main() 文件 「download_page.py」,線路118,在主 insert_urls(dbconn,文件名) 文件「download_page.py」,第95行,在insert_urls中 dbconn.execute_query(sql) execute_query中的第127行文件「C:\ performance \ Iperf \ dbconn.py」 self.cursor.commit() AttributeError:'光標'對象沒有'提交'屬性 – cybertextron

+0

糟糕,這應該是'self.conn.commit()'。固定。 –