Created
May 15, 2014 22:01
-
-
Save goyuninfo/63c3729c31c3e0a34d3b to your computer and use it in GitHub Desktop.
Example of Python Script to deal with MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import MySQLdb | |
conn = MySQLdb.connect (host = "host", | |
user = "us", | |
passwd = "pass", | |
db = "yourDB") | |
cursor = conn.cursor (MySQLdb.cursors.DictCursor ) | |
cursor.execute ("SELECT * from yourTable") | |
rows = cursor.fetchall () | |
for row in rows: | |
print (row["name"], row["description"]) | |
cursor.execute (' insert into anotherTable (id,name, description,price) values (6800, %s, %s, %s)', (row["name"], row["description"], 8.88)) | |
print "number of all: %d" % cursor.rowcount | |
cursor.close () | |
conn.commit() | |
conn.close () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment