0

我正在从 sqlite3 迁移到 MySQL。

使用 sqlite 时,默认的表顺序是添加行的时间 - 底部的最新行。如:

sqlite> select * from Trial_Balance;

+------------------------+--------+--------+
| General_Ledger_Account | DR     | CR     |
+------------------------+--------+--------+
| Opening Balances       |   0.00 | 200.00 |
| B1                     |   0.00 | 299.00 |
| I2                     |   0.00 |  20.00 |
| X3                     | 300.00 |   0.00 |
| TOTAL                  | 519.05 | 519.05 |
+------------------------+--------+--------+

迁移到 MySQL 时,默认表顺序按第一列的字母顺序给出:

mysql> select * from Trial_Balance;
+------------------------+--------+--------+
| General_Ledger_Account | DR     | CR     |
+------------------------+--------+--------+
| B1                     |   0.00 | 299.00 |
| I2                     |   0.00 |  20.00 |
| Opening Balances       |   0.00 | 200.00 |
| TOTAL                  | 519.05 | 519.05 |
| X3                     | 300.00 |   0.00 |
+------------------------+--------+--------+

我找不到一个 ORDER BY 可以满足按“添加订单”进行排序的想法?是否存在“添加订单”的订单?

或者,您将如何实现这一目标?您是否会创建一个时间数据类型列,该列在添加新条目时自动填充,然后按此排序?我会很感激你的回答中的一个例子。

谢谢你的帮助。

我的完整代码:

def s_exe(sql):
    mycursor.execute(sql)

s_exe('''CREATE TABLE Trial_Balance
                        (General_Ledger_Account VARCHAR(20) PRIMARY KEY,
                        DR DECIMAL(13,2),
                        CR DECIMAL(13,2))''')

def tb(tb):
    mycursor.execute(("INSERT INTO Trial_Balance (General_Ledger_Account, DR, CR) VALUES (%s,%s,%s)"),((tb.General_Ledger_Account, tb.DR, tb.CR)))

def select_one(variables,tablename): #variables can be multiple e.g. 'DRcode,DR'
    mycursor.execute("SELECT {} FROM {}".format(variables,tablename))
    return mycursor.fetchone()

def create_trial_balance():
    res = select_one('SUM(DR),SUM(CR)','Opening_Balances')
    try:
        if res[0] >= res[1]: # if sum DR is greater or equal to sum CR
            tb(TB('Opening Balances','',str(res[0]-res[1])))
        else:
            tb(TB('Opening Balances',str(res[1]-res[0]),''))
    except:
        pass
    for n in return_gen_ledg_codes(): #DR and CR balances from every General Ledger
        sumDR = select_one('SUM(DR)',n)[0]
        sumCR = select_one('SUM(CR)',n)[0]
        if sumCR == None and sumDR != None:
            tb(TB(n,str(sumDR),''))
        elif sumDR == None and sumCR != None:
            tb(TB(n,'',str(sumCR)))
        elif sumDR >= sumCR:
            tb(TB(n,str(sumDR-sumCR),''))
        else:
            tb(TB(n,'',str(sumCR-sumDR)))
    try:
        res = select_one('SUM(DR),SUM(CR)','Trial_Balance') # SUM of Trial Balance
        tb(TB('TOTAL',str(res[0]),str(res[1])))          
    except:
        pass
4

0 回答 0