Python Database call

Source: Internet
Author: User

#!/usr/bin/python
#coding =utf-8


Import MySQLdb
Import Sys

Db_username= ' Scto '
db_pass= ' xjtb2016 '
#DB_CONF_PATH =
Db_host= ' 127.0.0.1 '
Db_port= ' 3306 '
Db_basename= ' Test '
db_char_set= ' UTF8 '

#连接数据库
Def get_connect ():
Return MySQLdb.connect (Host=db_host,user=db_username,passwd=db_pass,db=db_basename,charset=db_char_set)

#获取cursor
DEF get_cursor (conn):
Return Conn.cursor ()
#打印
def show_print (Conn,sql):
cursor = GET_CURSOR (conn)
Re = cursor.execute (sql)
For row in Cursor.fetchall ():
#print '%s '%row
Print (ROW)
#close (Cursor,conn)

#关闭数据库连接
DEF conn_close (conn):
If conn! = None:
Conn.close ()
#关闭cursor方法
def cursor_close (cursor):
If cursor!=none:
Cursor.close ()

#全部服务关闭
def close (Cursor,conn):
Cursor_close (cursor)
Conn_close (conn)

#查看数据库表情况
DEF get_databases (conn):
Show_print (conn, "show Databases")

#查看数据库中的表
DEF get_tables (conn):
#sql = "Show Tables"
Show_print (conn, "Show Tables")

#查看数据库启动时间
DEF get_mysqluptime (conn):
Show_print (conn, "show global status like ' uptime ';")

#插入表数据
def insert_table (conn):
Print Welcome to insert table Data
Print <<<----------------------------
#sql = "INSERT into HH (id,name,age) VALUES (%s,%s,%s)"
sql = "INSERT into HH (id,info) VALUES (2, ' Hej ')"
params = (' 2 ', ' Hej ', ' $ ')
Print---------------------------->>>>
cursor = get_cursor (conn)
Cursor.execute (sql)
#result = Cursor.execute (sql,params)
Conn.commit ()
#close (cursor,conn)
#return Result
#查看表中相关数据
def query_table (conn,table):
If Table! = ':
sql = ' select * from ' + table
print ' You want to view the The body statement is: \ t%s "%sql
Show_print (conn,sql)
Else:
Print (" The table that you want to view \ T "+table)
#更新表数据
def update_table ():
sql= ' update student set name =%s where id = 1 '
params = ("Hehaha")
conn = Get_connect ()
cursor = Get_curso R (conn)
result = Cursor.execute (sql, params)
Conn.commit ()
Close (cursor, conn)
return result

#创建表
def create_table (tablename):
If tablename! = ":

conn = Get_connect ()
cursor = GET_CURSOR (conn)
Cursor.execute (' CREATE TABLE ' +tablename+ ' (ID int,info varchar (20)) ')
Conn.commit ()
Else
Print ("You did not enter the database table name you want to create \ T" +tablename)
#cursor_close (cursor)

#查看连接信息

Def print_conn_info ():
Print ("Database connection data: \ t" + db_host+ ' \ t ' + db_username + ' \ t ' + Db_pass + ' \ t ' + db_basename)

def delete_date (conn,tablename,datasize):
cursor = GET_CURSOR (conn)
Cursor.execute (' Delete from ' +tablename+ ' where id= ' +datasize)
Conn.commit ()

Def get_inputinfo ():
Print "Please enter: \t\n"
Result=raw_input ()
Print ("Your input is: \t\n" +result)
return result


def main ():
#获取标准输入
Result=get_inputinfo ()

#连接数据库
Conn=get_connect ()
#查看数据库表情况
Get_databases (conn)
#查看数据库中的表
Get_tables (conn)
#查看数据库启动时间
GET_MYSQLUPTIME (conn)
#插入表数据
Insert_table (conn)
Print "Please enter the name of the database table you want to query"
Sql=raw_input ()
#查看表中相关数据
Query_table (Conn,sql)
Query_table (conn, "student")
#修改数据
Update_table ()
Print "Please enter the name of the database table you want to create"
Tablename=raw_input ()
#创建表
Create_table (tablename)

#查看连接信息
Print_conn_info ()
#删除指定表中的指定数据
Print "Please enter the name of the database table you want to delete \ t"
Tablename=raw_input ()
Print "Please enter the data you want to delete"
Datasize=raw_input ()
Delete_date (Conn,tablename,datasize)

Conn_close (conn)

If __name__== ' __main__ ':
Main ()

Python Database call

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.