Curso de Programación en Python/MySQL-3
								
								Ir a la navegación
				Ir a la búsqueda
				
					
								
							
		MySQL-3.py
 1 #!/usr/bin/python
 2 #-*-coding: utf-8 -*-
 3 
 4 import mysql.connector
 5 from mysql.connector import errorcode
 6 
 7 config = {
 8   'user': 'PythonClase',
 9   'password': 'Py800se',
10   'host': '127.0.0.1',
11   'database': 'PythonClase',
12   'raise_on_warnings': True,
13   'unix_socket': '/var/lib/mysql/mysql.sock'
14 }
15 
16 try:
17   Conn = mysql.connector.connect(**config)
18 except mysql.connector.Error as err:
19   if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
20     print( "UsuarioNombre o Contraseña incorrecto" )
21   elif err.errno == errorcode.ER_BAD_DB_ERROR:
22     print( "Base de Datos no existe" )
23   else:
24     print( err )
25 
26 Cursor = Conn.cursor()   
27 
28 Query = "CREATE TABLE TablaDePrueba( id int NOT NULL AUTO_INCREMENT, \
29                              CampoDePueba varchar(75), \
30                              PRIMARY KEY(id))";
31 
32 Cursor.execute( Query )
33 
34 Conn.commit()
35 
36 Conn.close()
37 
38 print( "Mira en to carpeta para ver que tienen ustedes una table nueva" )
39 print( "el mysql console para verlo" );
Resultado
[rrc@Pridd PythonClase]$ ./MySQL-3.py Mira en to carpeta para ver que tienen ustedes una table nueva el mysql console para verlo [rrc@Pridd PythonClase]$ mysql -pPy800se -u PythonClase 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)]> use PythonClase; Database changed MariaDB [PythonClase]> show tables; +-----------------------+ | Tables_in_PythonClase | +-----------------------+ | TablaDePrueba | +-----------------------+ 1 row in set (0.00 sec) MariaDB [PythonClase]>

