How can the dictionary read by python from the database be more easily inserted into the database?

find the easiest way to read all the data from the database in dictionary format, and then insert the database with the same structure.

1
21020-30
3insert into table () values 
4thinkphp   $sql->data($arr)->add();  python
Dec.27,2021

if you only want to quickly generate insert statements, consider writing a general method to automatically produce sql statements according to the dictionary:



def dict_2_sql(dicts,db):
    names=','.join(dicts.keys())
    values=','.join(dicts.values())
    sql='insert into '+db+"("+names+") values("+values+")"
    return sql
if __name__ == '__main__':
    test = {
        'a': '1',
        'b': '2',
        'c': '3',
        'd': '4',
    }
    sql=dict_2_sql(test,"dbname")
    print(sql)

print result:

insert into dbname(a,b,c,d) values(1,2,3,4)

first of all, one is the solution of the php framework, the other is the Python language, the two are not related, you can say whether flask or Django has a similar method to tp.
secondly, I think the underlying implementation of $sql- > data ($arr)-> add () also generates a sql statement. After all, it takes too much time to establish a sql link.
third, you can use

.
select * into  from 

this sql statement completely copies a table


other SQL is not clear, pymysql does not have similar functions.
when it comes to frameworks, I don't remember any of the ORM, commonly used by python that provide a similar way of writing.
since no one has written it, and you think you can use it so easily in php, you might as well implement one according to php's idea.
I wrote a database class and some utility functions myself. My code for the logic you said goes something like this:

data = database('test_db').query_dict("SELECT * FROM `table`")
generic_insert(data, 'insert_table')
The

insert database is written dead in a function. If you have multiple database maintenance, consider abstracting it again and implementing it in the database class. The logic is about:

-sharp data is a dict
db = database('test_db').get_db()
db.insert(data, 'insert_table')
db.close()

share your own insert function.

from database import database
-sharp pymysql

def generic_insert(data, target_table):
    -sharp 
    -sharp mongo
    -sharp 20000, 200002-3M
    -sharp $todo: mysqldriver, insert stmt, insert
    i = 0
    count = 0
    con = database('test_database').get_db()
    cur = con.cursor()

    key_name = data[0].keys()
    
    while i < len(data):
        subdata = [tuple(x.values()) for x in data[i:(i + 20000)]]
    
        insert_stmt = "INSERT INTO `{target_table}` (`{colname}`) VALUES ({values})".format(
            target_table=target_table,
            colname="`,`".join(key_name),
            values=','.join(['%s' for x in range(len(key_name))])
        )

        try:
            rows= cur.executemany(insert_stmt, subdata)
            count += rows
            con.commit()
        except Exception as e:
            print('INSERT ROWS: {}'.format(count))
            cur.close()
            con.close()
            raise e
        i += 20000
        
    cur.close()
    con.close()
    return count
  • Mysql queries missing data

    member_id openID mp_id openID openID ** mp_id openID ** sql SELECT * FROM `score_log` WHERE `mp_id` = gh_1d3037ae656c AND `openid` = o5NHFsy-PUHxY7G_h_S8UscpKVg8 ...

    May.30,2022
Menu