Skip to content

Instantly share code, notes, and snippets.

@goyuninfo
Created May 15, 2014 22:01
Show Gist options
  • Save goyuninfo/63c3729c31c3e0a34d3b to your computer and use it in GitHub Desktop.
Save goyuninfo/63c3729c31c3e0a34d3b to your computer and use it in GitHub Desktop.
Example of Python Script to deal with MySQL
#!/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