Skip to content

Instantly share code, notes, and snippets.

@camark
Forked from maltzsama/pymysqlconn.py
Created June 6, 2016 01:02
Show Gist options
  • Select an option

  • Save camark/0b9bb52aa51af508a9039d74b6bd0e06 to your computer and use it in GitHub Desktop.

Select an option

Save camark/0b9bb52aa51af508a9039d74b6bd0e06 to your computer and use it in GitHub Desktop.
python working with mysql
#!/usr/bin/python
# versao.py – captura e mostra a versão do MySQL database server.
# importe os modulos do MySQLdb e sys
import MySQLdb
import sys
# Abra uma conexão com o banco de dados
# Tenha certeza de ter trocado o IP, usuario, senha e database para os seus.
connection = MySQLdb.connect (host = “192.168.1.1″, user = “username”, passwd = “password”, db = “database_name”)
# Inicie um cursor usando o método cursor()
cursor = connection.cursor ()
# Execute a expressão SQL
cursor.execute (“SELECT VERSION()”)
# capture a informação com o método fetchone().
row = cursor.fetchone ()
# mostre a informação[0]
print “Server version:”, row[0]
# Feche o cursor
cursor.close ()
# Feche a conexão
connection.close ()
# Saia do programa.
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment