[英]How to make a new SQL table with extracted SQL table data?
I'm a beginner of SQlite , and about to make a new table with extracted table data.我是 SQlite 的初学者,即将用提取的表数据制作一个新表。 I learned the way of making a new table from an existing table like this;
我学会了从这样的现有表中制作新表的方法;
create table T2 select * from T1
so, I guess I could make a new table with extracted data like this;所以,我想我可以用这样的提取数据制作一个新表;
import pandas as pd
import sqlite3
df = pd.DataFrame(
data={'column1': [10, 20, 30, 40],
'column2': [0, 1, 1, 0],
'column3': ['a', 'b', 'a', 'b']})
dbname = 'TEST.db'
con = sqlite3.connect(dbname)
cur = con.cursor()
df.to_sql("T1",con,if_exists="replace",method="multi",chunksize=1000)
con.execute("create table if not exists T2 As (SELECT column1,column3 FROM T1 WHERE column3='a' Group by column2)")
df_sqlquery = pd.read_sql_query(sql="SELECT * FROM T2",con=con)
print(df_sqlquery)
but, it didn't work and I got the error message;但是,它不起作用,我收到了错误消息; "OperationalError: near "(": syntax error" To make the new table, should I output the extracted table with pandas.read_sql and input it to SQlite with df.to_sql? I couldn't find the good answer. I'd appreciate it if you could help me.
"OperationalError: near "(": syntax error" 为了创建新表,我是否应该使用 pandas.read_sql 输出提取的表并使用 df.to_sql 将其输入到 SQlite?我找不到好的答案。我会很感激如果你能帮助我的话。
Just change this:只需更改此:
create table if not exists T2
As (SELECT column1,column3 FROM T1 WHERE column3='a' Group by column2)
into this:进入这个:
create table if not exists T2
As SELECT column1,column3 FROM T1 WHERE column3='a' Group by column2
That is why you get the error.这就是你得到错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.