Python operation MySQL Database

Source: Internet
Author: User
Python operation MySQL Database related operation example

#-*-Coding:utf-8-*-#python operate mysql databaseimport mysqldb #数据库名称DATABASE_NAME = ' #host = ' localhost ' or ' 172.0 .0.1 ' HOST = ' #端口号PORT = ' #用户名称USER_NAME = ' #数据库密码PASSWORD = ' #数据库编码CHAR_SET = ' #初始化参数def init (): Global DATABASE    _name database_name = ' test ' global host host = ' localhost ' global port port = ' 3306 ' global user_name user_name = ' root ' global PASSWORD PASSWORD = ' root ' global char_set char_set = ' UTF8 ' #获取数据库连接def get_ Conn (): Init () return MySQLdb.connect (host = host, user = user_name, passwd = PASSWORD, db = database_name, CharSet        = Char_set) #获取cursordef get_cursor (conn): Return Conn.cursor () #关闭连接def conn_close (conn): if conn! = None: Conn.close () #关闭cursordef cursor_close (cursor): if cursor! = None:cursor.close () #关闭所有def Close (CURSOR, conn ): Cursor_close (cursor) conn_close (conn) #创建表def create_table (): sql = ' CREATE TABLE ' student ' (' ID ' I NT (one) not NULL, ' NamE ' varchar () not NULL, ' age ' int (one) DEFAULT NULL, PRIMARY key (' id '), UNIQUE key ' name ' (' name ')) engine=i Nnodb DEFAULT CHARSET=UTF8 ' conn = get_conn () cursor = GET_CURSOR (conn) result = Cursor.execute (sql) con N.commit () Close (cursor, conn) return result #查询表信息def query_table (TABLE_NAME): if table_name! = ': SQL = ' SELECT * FROM ' + table_name conn = Get_conn () cursor = GET_CURSOR (conn) result = Cursor.execute (s QL) for row in Cursor.fetchall (): Print (row) #for R in row: #循环每一条数据 #pri  NT (r) Close (cursor, conn) else:print (' Table name is empty! ') #插入数据def insert_table (): sql = ' INSERT into student (ID, name, age) values (%s,%s,%s) ' params = (' 1 ', ' hongten_a ', ' + ') conn = Get_conn () cursor = GET_CURSOR (conn) result = Cursor.execute (sql, params) conn.commit () Close (c URSOR, conn) return result #更新数据def update_table (): sql = 'Update student Set name =%s where id = 1 ' params = (' Hongten ') conn = Get_conn () cursor = GET_CURSOR (conn) re Sult = cursor.execute (sql, params) conn.commit () Close (cursor, conn) return result #删除数据def delete_data (): Sq L = ' Delete from student where id =%s ' params = (' 1 ') conn = Get_conn () cursor = GET_CURSOR (conn) result = CU Rsor.execute (SQL, params) conn.commit () Close (cursor, conn) return result #数据库连接信息 def print_info (): Print (' Database connection information: ' + database_name + HOST + PORT + user_name + PASSWORD + char_set) #打印出数据库中表情况def show_databases (): sql = ' Sho w databases ' conn = Get_conn () cursor = GET_CURSOR (conn) result = Cursor.execute (SQL) for row in Cursor.fetcha LL (): Print (Row) #数据库中表情况def show_tables (): sql = ' show tables ' conn = Get_conn () cursor = GET_CU RSOR (conn) result = Cursor.execute (SQL) for row in Cursor.fetchall (): Print (ROW) def main (): Show_ta Bles () #创建表 result = Create_table () print (result) #查询表 query_table (' student ') #插入数据 print (insert_table ()) print (' After inserting data ....    ') query_table (' student ') #更新数据 print (' After updating data ... ') query_table (' Student ') #删除数据 Delete_data () print (' delete data ... ') query_table (' Student ') print_info () #数据库中表情况 show_tables () if __na me__ = = ' __main__ ': Main ()
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.