-
-
Save camark/0b9bb52aa51af508a9039d74b6bd0e06 to your computer and use it in GitHub Desktop.
python working with mysql
This file contains hidden or 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 | |
| # 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