Python writes sqlserver,oracle,mysql data queries and inserts data

Source: Internet
Author: User
Tags uuid

SQL Server Method:

Insert Data Code Demo ( context Manager method ):

Import Pymssql,uuid
From class_area.class_readconf import readdate #导入读取配置文件模块方法
Sql_data=readdate (' sqlserver.conf ', ' DATABASE ', ' Config '). ReadData ()
Read_data=readdate (' area.conf ', ' areas ', ' Config '). ReadData ()
Area_read_2=eval (Read_data)
# Server database servers name or IP
# User Username
# password Password
# database name '
Class SQL Server ():
def insert_sql_server (self):
With Pymssql.connect (eval (sql_data) [' Server '],eval (sql_data) [' User '],eval (sql_data) [' Password '],eval (sql_data) [ ' Database ']) as Conn:
With Conn.cursor () as cursor:
For Data_row in range (len (area_read_2)):
area_code=area_read_2[data_row][' Code ']
area_name=area_read_2[data_row][' Name ']
Center_point= ",". Join ((area_read_2[data_row][' latitude '],area_read_2[data_row][' longitude '))
area_parentcode=area_read_2[data_row][' Parentcode ']
Sql= (' INSERT into T_sys_area (id,area_code,area_name,center_point,area_parentcode) VALUES (%s,%s,%s,%s,%s) '% (' \ ' + STR (UUID.UUID1 ()). Replace ('-', ') + ' \ ', ' \ ' +area_code+ ' \ ', ' \ ' +area_name+ ' \ ', ' \ ' +center_point+ ' \ ', ' \ ' + Area_parentcode+ '))
Cursor.execute (SQL) #执行sql
Conn.commit () #提交数据
Print ("Success")
if __name__ = = ' __main__ ':
Sql_server_data=sqlserver ()
Sql_server_data.insert_sql_server ()

Oracle methods:

To read a data method:

#!-*-Encoding:utf-8-*-
Import Cx_oracle,uuid
From class_area.class_readconf import readdate
Arearead_1=eval (readdate (' oracle.conf ', ' DATABASE ', ' config_1 '). ReadData ())
Area_read=readdate (' area.conf ', ' areas ', ' Config '). ReadData ()
Area_read_2=eval (Area_read)
Class Mysqlquerydata:
def __init__ (self,mysqlsentence):
Self.mysqlsentence = Mysqlsentence
def mysqlconnect (self):
#连接数据库
#strConnection = eval (arearead_1)
#conn = Cx_oracle.connect (' User name/password @ Database IP: Port number/database name ')
#cnn =cx_oracle.connect (' Newleader/[email protected]:1521/orcl ')
data_1=arearead_1[' user ']+ '/' +arearead_1[' password ']+ ' @ ' +arearead_1[' host ']+ ': ' +arearead_1[' Port ']+ '/' + arearead_1[' database '
Cnn=cx_oracle.connect (data_1)
#使用cursor方法获取游标
Cursor=cnn.cursor ()
#cursor =cnn.cursor (buffered=true) #当SQL查询语句是查询所有的, and then call the Fetchone () method, then you need to add buffered=true, otherwise it will be an error.
# Execute SQL statements using the Execute method
Cursor.execute (self.mysqlsentence)
#使用fetchall方法获取所有数据
Data=cursor.fetchall ()
#读取一条数据
#data =cursor.fetchone ()
#关闭游标
Cursor.close ()
#关闭数据库连接
Cnn.close ()
Return data
if __name__ = = "__main__":
Mysqlquerydata = Mysqlquerydata ("SELECT * from T_sys_area")
Print (Mysqlquerydata.mysqlconnect ())

Insert Data method:
Class Oracleinsertdata ():
def oracleconnect (self):
data_1=arearead_1[' user ']+ '/' +arearead_1[' password ']+ ' @ ' +arearead_1[' host ']+ ': ' +arearead_1[' Port ']+ '/' + arearead_1[' database '
Cnn=cx_oracle.connect (data_1)
Cursor=cnn.cursor ()
For Data_row in range (len (area_read_2)):
area_code=area_read_2[data_row][' CODE ']
area_name=area_read_2[data_row][' NAME ']
Center_point= ",". Join ((area_read_2[data_row][' latitude '],area_read_2[data_row][' longitude '))
area_parentcode=area_read_2[data_row][' Parentcode ']
Sql= (' INSERT into T_sys_area (id,area_code,area_name,area_parentcode,center_point) VALUES (%s,%s,%s,%s,%s) '% (' \ ' + STR (UUID.UUID1 ()). Replace ('-, ') + ' \ ', ' \ ' +area_code+ ' \ ', ' \ ' +area_name+ ' \ ', ' \ ' +area_parentcode+ ' \ ', ' \ ' + Center_point+ '))
Cursor.execute (SQL)
Cnn.commit ()
Cursor.close ()
#关闭数据库连接
Cnn.close ()

Print ("Success")
if __name__ = = "__main__":
Oracleinsertdata=oracleinsertdata ()
Oracleinsertdata.oracleconnect ()

Python writes sqlserver,oracle,mysql data queries and inserts data

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.