Curso de Programación en Python/MySQL-1
Ir a la navegación
Ir a la búsqueda
MySQL-1.py
urpmi mysql-connector-python
Argument Name | Default | Description |
---|---|---|
user | The user name used to authenticate with the MySQL server. | |
password | The password to authenticate the user with the MySQL server. | |
database | The database name to use when connecting with the MySQL server. | |
host | 127.0.0.1 | The host name or IP address of the MySQL server. |
port | 3306 | The TCP/IP port of the MySQL server. Must be an integer. |
unix_socket | The location of the Unix socket file. | |
auth_plugin | Authentication plugin to use. Added in 1.2.1. | |
use_unicode | True | Whether to use Unicode. |
charset | utf8 | Which MySQL character set to use. |
collation | utf8_general_ci | Which MySQL collation to use. |
autocommit | False | Whether to autocommit transactions. |
time_zone | Set the time_zone session variable at connection time. | |
sql_mode | Set the sql_mode session variable at connection time. | |
get_warnings | False | Whether to fetch warnings. |
raise_on_warnings | False | Whether to raise an exception on warnings. |
connection_timeout | Timeout for the TCP and Unix socket connections. | |
client_flags | MySQL client flags. | |
buffered | False | Whether cursor objects fetch the results immediately after executing queries. |
raw | False | Whether MySQL results are returned as is, rather than converted to Python types. |
ssl_ca | File containing the SSL certificate authority. | |
ssl_cert | File containing the SSL certificate file. | |
ssl_key | File containing the SSL key. | |
ssl_verify_cert | False | When set to True, checks the server certificate against the certificate file specified by the ssl_ca option. Any mismatch causes a ValueError exception. |
force_ipv6 | False | When set to True, uses IPv6 when an address resolves to both IPv4 and IPv6. By default, IPv4 is used in such cases. |
dsn | Not supported (raises NotSupportedError when used). | |
pool_name | Connection pool name. Added in 1.1.1. | |
pool_size | 5 | Connection pool size. Added in 1.1.1. |
pool_reset_session | True | Whether to reset session variables when connection is returned to pool. Added in 1.1.5. |
compress | False | Whether to use compressed client/server protocol. Added in 1.1.2. |
converter_class | Converter class to use. Added in 1.1.2. | |
fabric | MySQL Fabric connection arguments. Added in 1.2.0. | |
failover | Server failover sequence. Added in 1.2.1. | |
option_files | Which option files to read. Added in 2.0.0. | |
option_groups | ['client', 'connector_python'] | Which groups to read from option files. Added in 2.0.0. |
allow_local_infile | True | Whether to enable LOAD DATA LOCAL INFILE. Added in 2.0.0. |
Antes de iniciar, debemos crear el base de datos [rrc@Pridd PythonClase]$ mysql -p Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 13 Server version: 10.0.22-MariaDB Mageia MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> create database PythonClase; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | PythonClase | | information_schema | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> show create database PythonClase; +-------------+----------------------------------------------------------------------------------------------+ | Database | Create Database | +-------------+----------------------------------------------------------------------------------------------+ | PythonClase | CREATE DATABASE `PythonClase` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */ | +-------------+----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) MariaDB [(none)]> grant all on PythonClase.* to 'PythonClase'@'localhost' identified by 'Py800se'; Query OK, 0 rows affected (0.00 sec)
1 #!/usr/bin/python
2 #-*-coding: utf-8 -*-
3
4 import mysql.connector
5 from mysql.connector import errorcode
6
7 try:
8 Conn = mysql.connector.connect( user='PythonClase',
9 password='Py800se',
10 host='127.0.0.1',
11 unix_socket='/var/lib/mysql/mysql.sock',
12 database='PythonClase')
13 except mysql.connector.Error as err:
14 if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
15 print( "UsuarioNombre o Contraseña incorrecto" )
16 elif err.errno == errorcode.ER_BAD_DB_ERROR:
17 print( "Base de Datos no existe" )
18 else:
19 print( err )
20 else:
21 Conn.close()
22
23 pip3 install mysql-connector-python --allow-external mysql-connector-python ---- NO SIRVE MÁS ----
24
25 Descarga URL: http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.4.zip#md5=3df394d89300db95163f17c843ef49df
26
27 unzip
28
29 python3 setup.py install