基于问题“psycopg2:使用一个查询插入多行”(psycopg2: insert multiple rows with one query)
从JS,我在服务器端接收以下内容:“2,3,4 ......”
然后,在服务器端(python 3.6):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.andre.nutridian.Home"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
<TextView
android:layout_width="359dp"
android:layout_height="50dp"
android:singleLine="true"
android:text="MENIU"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.Material.Medium"
android:textColor="@color/colorPrimary"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="@+id/tabel"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@drawable/cerclebackgroundpurple"
android:padding="10dp"
android:src="@drawable/ic_accessibility_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Banking"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Raport"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/dieta"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@drawable/cerclebackgroundpink"
android:padding="10dp"
android:src="@drawable/ic_pie_chart_outlined_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Ideas"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Diete"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:id="@+id/calcul"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@drawable/cerclebackgroundgreen"
android:padding="10dp"
android:src="@drawable/ic_today_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Add"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Liste alimente calorii"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/statistici"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@drawable/cerclebackgroundyello"
android:padding="10dp"
android:src="@drawable/ic_trending_up_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Ideas"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="10dp"
android:background="@color/lightgray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Statistici"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
控制台上的输出:[“(2,'截图来自2018-05-29 07-13-47.png,1')”,“(3,'截图来自2018-05-29 07-13-47 .png,1')“,”(4,'截图来自2018-05-29 07-13-47.png,1')“]
错误:INSERT的表达式多于目标列
我请你指导我。如何创建正确的列表/元组并将数据插入数据库?
答案 0 :(得分:1)
你可以这样试试吗:
list_urlquery[i] = """({}, '{}', '{}')""".format(str(list_urlquery[i]).strip(), str(file.filename).strip(), str(PATH_ID).strip())
我假设只有theid列是整数。
如果pathid也是int(数字),则尝试下面的
list_urlquery[i] = """({}, '{}', {})""".format(str(list_urlquery[i]).strip(), str(file.filename).strip(), str(PATH_ID).strip())
答案 1 :(得分:0)
根据文档(http://initd.org/psycopg/docs/extras.html#fast-exec),它说,&#34; argslist - 序列序列......&#34;
因此,序列序列==元组列表
# creating list of tuples (sequence of sequences).
list_urlquery = urlquery_partition.strip().split(",") # list for "theid"
lst_filename = [] # list for "thefilename"
lst_pathid = [] #list for "thepathid"
lst_tpl_insertdata = [] # creating "List of Tuples == Sequence of sequences".
for i in range(len(list_urlquery)):
lst_filename.append(str(file.filename).strip())
lst_pathid.append(PATH_ID)
#combine all list using "zip()" for creating "List of Tuples == Sequence of sequences".
lst_tpl_insertdata = list(zip(list_urlquery,lst_filename,lst_pathid))
insert_query = 'INSERT INTO tbl_ma (theid, thefilename, thepathid) VALUES %s'
psycopg2.extras.execute_values(cursor, insert_query, lst_tpl_insertdata, template=None, page_size=100)
connection.commit()