【问题标题】:Load data from data frame into SQLite table将数据框中的数据加载到 SQLite 表中
【发布时间】:2021-05-01 07:28:01
【问题描述】:

所以我在文本文件中有这样的数据:

TRMMQMI12903CE2A25<SEP>SOCBYZN12AB0189CFC<SEP>Arthur Adams<SEP>Callin' Heaven
TRMMQUA128F425761E<SEP>SOEDQXW12A8AE458B8<SEP>Wesley Willis<SEP>Jesus Christ
TRMMQXZ128F92D0A0E<SEP>SOQVOIM12A8C140DF9<SEP>Manjul<SEP>Hungry Belly
TRMMZRE128F42B799E<SEP>SOHLRUF12A8C13F0FF<SEP>Daniel Balavoine<SEP>Sauver L'Amour
TRMMZJO128F42434B2<SEP>SOTFIKQ12A6D4FB45F<SEP>Sandhy SonDoro<SEP>Don�t Let It Bring You Down
TRMMZSA128F4233C55<SEP>SOBXHIT12A8C130F59<SEP>Willie "Big Eyes" Smith<SEP>Tell Me Mama

我设法像这样将它加载到熊猫数据框中:

data = pd.read_table('unique_tracks.txt', header=None, sep='<SEP>', engine='python')

我从中得到的数据如下所示:

0       TRMMMYQ128F932D901  ...                         Silent Night
1       TRMMMKD128F425225D  ...                          Tanssi vaan
2       TRMMMRX128F93187D9  ...                    No One Could Ever
3       TRMMMCH128F425532C  ...                        Si Vos Quer�s
4       TRMMMWA128F426B589  ...                     Tangle Of Aspens

我也这样创建了我的表:

import sqlite3
from sqlite3 import Error
import pandas as pd


def create_connection(db_file):
    """ create a database connection to the SQLite database
        specified by db_file
    :param db_file: database file
    :return: Connection object or None
    """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)

    return conn

def create_table(conn, create_table_sql):
    """ create a table from the create_table_sql statement
    :param conn: Connection object
    :param create_table_sql: a CREATE TABLE statement
    :return:
    """
    try:
        c = conn.cursor()
        c.execute(create_table_sql)
    except Error as e:
        print(e)

def main():
    database = r"Data_db.db"

    sql_create_songs_table = """ CREATE TABLE IF NOT EXISTS songs (
                                        id integer PRIMARY KEY,
                                        execution_id text NOT NULL,
                                        song_id text NOT NULL,
                                        artist_id text NOT NULL,
                                        song_title text NOT NULL
                                    ); """

    # create a database connection
    conn = create_connection(database)

    # create tables
    if conn is not None:
        # create projects table
        create_table(conn, sql_create_songs_table)

        # create tasks table
        create_table(conn, sql_create_users_table)
    else:
        print("Error! cannot create the database connection.")

if __name__ == '__main__':
    main()

create_connection(r"Data_db.db")

我的问题是如何将数据从我的数据框中加载到表中?在将数据加载到数据库时,pd.read_table 和 pd.read_csv 有区别吗?

【问题讨论】:

    标签: python pandas database sqlite


    【解决方案1】:

    你可以使用 to_sql 方法。

    data.to_sql("songs", conn)
    

    查看文档https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html

    read_table 和 read_csv 应该没有区别,因为它已经在 pandas 数据框中。只需确保数据框列与表列匹配即可。

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多