Initial commit
This commit is contained in:
53
test_db_connection.py
Normal file
53
test_db_connection.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import mysql.connector
|
||||
from mysql.connector import errorcode
|
||||
|
||||
# 資料庫連線資訊
|
||||
DB_HOST = "mysql.theaken.com"
|
||||
DB_PORT = 33306
|
||||
DB_NAME = "db_A018"
|
||||
DB_USER = "A018"
|
||||
DB_PASSWORD = "4MQYkJRYtyLE"
|
||||
|
||||
def create_table():
|
||||
try:
|
||||
# 建立資料庫連線
|
||||
conn = mysql.connector.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
database=DB_NAME
|
||||
)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 如果資料表已存在,先刪除
|
||||
cursor.execute("DROP TABLE IF EXISTS pizza")
|
||||
|
||||
# 建立資料表
|
||||
create_table_query = (
|
||||
"CREATE TABLE pizza ("
|
||||
" id INT AUTO_INCREMENT PRIMARY KEY,"
|
||||
" name VARCHAR(50),"
|
||||
" size VARCHAR(50),"
|
||||
" price INT"
|
||||
")"
|
||||
)
|
||||
cursor.execute(create_table_query)
|
||||
|
||||
print("資料表建立成功!")
|
||||
|
||||
except mysql.connector.Error as err:
|
||||
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
||||
print("使用者名稱或密碼錯誤")
|
||||
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
||||
print("資料庫不存在")
|
||||
else:
|
||||
print(err)
|
||||
finally:
|
||||
if 'cursor' in locals():
|
||||
cursor.close()
|
||||
if 'conn' in locals():
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_table()
|
Reference in New Issue
Block a user