Excel导入数据库
参考链接
读取MySQL并显示在QTableWidget
参考链接
class WindowLogic ( QMainWindow, uiData) :
def __init__ ( self) :
super ( WindowLogic, self) . __init__( )
self. setupUi( self)
self. run( )
'''设置按钮逻辑操作'''
def run ( self) :
self. return_pushButton. clicked. connect( self. returnWindow)
self. connect_database_pushButton. clicked. connect( self. connectDatabase_)
self. _translate = QtCore. QCoreApplication. translate
'''连接数据库函数'''
def Table_Data ( self, i, j, data) :
item = QtWidgets. QTableWidgetItem( )
self. tableWidget. setItem( i, j, item)
item = self. tableWidget. item( i, j)
item. setText( self. _translate( "Form" , str ( data) ) )
def connectDatabase_ ( self) :
HostName = self. host_name_lineEdit. text( )
Port = self. port_lineEdit. text( )
UserName = self. user_name_lineEdit. text( )
PassWd = self. pwd_lineEdit. text( )
DatabaseName = self. database_name_lineEdit. text( )
db = DatabaseOperations. dataImport_get_conn( HostName, int ( Port) , UserName, PassWd, DatabaseName)
'''读取数据库数据'''
cursor = db. cursor( )
cursor. execute( 'SELECT * FROM data' )
data = cursor. fetchall( )
print ( "数据查询成功" )
col_result = cursor. description
self. row = cursor. rowcount
self. vol = len ( data[ 0 ] )
col_result = list ( col_result)
self. tableWidget. setRowCount( self. row)
self. tableWidget. setColumnCount( self. vol)
a = 0
for i in col_result:
item = QtWidgets. QTableWidgetItem( )
self. data_tableWidget. setHorizontalHeaderItem( a, item)
item = self. data_tableWidget. horizontalHeaderItem( a)
item. setText( self. _translate( "Form" , i[ 0 ] ) )
a = a + 1
total = list ( data)
for i in range ( len ( data) ) :
total[ i] = list ( data[ i] )
for i in range ( self. row) :
for j in range ( self. vol) :
self. Table_Data( i, j, total[ i] [ j] )
DatabaseOperations. close_conn( db, cursor)
实现右侧下侧滑动条来控制数据显示
class MainWindowLogic ( QMainWindow, uiDataQuery) :
def __init__ ( self) :
super ( MainWindowLogic, self) . __init__( )
self. setupUi( self)
self. Scrollbar( )
self. run( )
'''滚轮滑动'''
def Scrollbar ( self) :
self. tableWidget. setHorizontalScrollBar( self. horizontalScrollBar)
self. tableWidget. setVerticalScrollBar( self. verticalScrollBar)