使用python

时间:2017-08-15 16:58:50

标签: python sql-server pandas sql-insert pandas-to-sql

我正在尝试使用dataframe.to_sql函数将pandas dataframe df插入SQL Server DB。但我得到以下错误:

源代码:

import pyodbc
import sqlalchemy
import urllib

df  #sample dataframe
params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=GIRSQL.GIRCAPITAL.com;DATABASE=Tableau;UID=SQL_User;PWD=pass")
engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
engine.connect()
df.to_sql(name='[Tableau].[dbo].[Test table]',con=engine, index=False, 
if_exists='append')

错误:

  

文件" C:\ Users \ Arvinth \ sqlalchemy \ engine \ default.py",第470行,在do_execute中       cursor.execute(语句,参数)

     

sqlalchemy.exc.ProgrammingError:(pyodbc.ProgrammingError)(' 42000'," [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]&#39附近的语法不正确; INTEGER'(102)(SQLExecDirectW)")[SQL:' \ nCREATE TABLE [[Tableau]。[dbo]。[Test table]](\ n \ t [A] INTEGER NULL,\ n \ t [B] INTEGER NULL,\ n \ t [C] INTEGER NULL \ n)\ n \ n']

示例数据框:

    A  B  C
 0  0  0  0
 1  1  1  1
 2  2  2  2
 3  3  3  3
 4  4  4  4
 5  5  5  5
 6  6  6  6
 7  7  7  7

任何人都可以帮助解决问题。

2 个答案:

答案 0 :(得分:4)

如评论所述,您在pandas'中引用了表 name arg的数据库名称和dbo模式。 to_sql要求' [Tableau]。[dbo]。[测试表]' 并括起名称,在sqlAlchemy引擎中生成错误CREATE TABLE SQL调用。由于当前连接引用的数据库且默认模式为dbo,因此不需要名称中的两个限定符: `' [Tableau]。[ DBO] '

因此,在df.to_sql中,只需引用不带括号转义的表名:

df.to_sql(name='Test table', con=engine, index=False, if_exists='append')

将在连接的dbo数据库的默认Tableau架构中创建所需的表:

CREATE TABLE [Test table] (
    [A] INTEGER NULL,
    [B] INTEGER NULL, 
    [C] INTEGER NULL
);

答案 1 :(得分:-2)

也许您可以参考NDK is missing a "platforms" directory. If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to /Users/jenkins/home/android-sdk/ndk-bundle. If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning. java.net.ConnectException: Connection refused (Connection refused) IOException: https://dl.google.com/android/repository/addons_list-3.xml java.net.ConnectException: Connection refused (Connection refused) IOException: https://dl.google.com/android/repository/addons_list-2.xml java.net.ConnectException: Connection refused (Connection refused) IOException: https://dl.google.com/android/repository/addons_list-1.xml java.net.ConnectException: Connection refused (Connection refused) Failed to download any source lists! FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':app'. > java.lang.NullPointerException (no error message) * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 中的表格:

Initialized native services in: /Users/jenkins/.gradle/native
Connected to daemon DaemonInfo{pid=6465, address=[3646567b-c480-4beb-a6d2-2ba0120d1cd8 port:54872, addresses:[/0:0:0:0:0:0:0:1, /127.0.0.1]], state=Idle, lastBusy=1506668208517, context=DefaultDaemonContext[uid=6c85c027-6030-4dfe-91b8-6a40166e80af,javaHome=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home,daemonRegistryDir=/Users/jenkins/.gradle/daemon,pid=6465,idleTimeout=10800000,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=AU,-Duser.language=en,-Duser.variant]}. Dispatching request Build{id=f5180918-cde6-414f-8694-c8777c5c6d05.1, currentDir=/Users/jenkins/home/ws/tankstream-android/tankstreamandroid}.
Received result org.gradle.launcher.daemon.protocol.BuildStarted@2d6c53fc from daemon DaemonInfo{pid=6465, address=[3646567b-c480-4beb-a6d2-2ba0120d1cd8 port:54872, addresses:[/0:0:0:0:0:0:0:1, /127.0.0.1]], state=Idle, lastBusy=1506668208517, context=DefaultDaemonContext[uid=6c85c027-6030-4dfe-91b8-6a40166e80af,javaHome=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home,daemonRegistryDir=/Users/jenkins/.gradle/daemon,pid=6465,idleTimeout=10800000,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=AU,-Duser.language=en,-Duser.variant]} (build should be starting).
The client will now receive all logging from the daemon (pid: 6465). The daemon log file: /Users/jenkins/.gradle/daemon/3.3/daemon-6465.out.log
Starting 9th build in daemon [uptime: 28 mins 20.507 secs, performance: 100%, no major garbage collections]
Executing build with daemon context: DefaultDaemonContext[uid=6c85c027-6030-4dfe-91b8-6a40166e80af,javaHome=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home,daemonRegistryDir=/Users/jenkins/.gradle/daemon,pid=6465,idleTimeout=10800000,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=AU,-Duser.language=en,-Duser.variant]
Creating new cache for plugin-use-metadata, path /Users/jenkins/.gradle/caches/3.3/plugin-resolution/plugin-use-metadata.bin, access org.gradle.cache.internal.DefaultCacheAccess@20ff28b
Creating new cache for client-status, path /Users/jenkins/.gradle/caches/3.3/plugin-resolution/client-status.bin, access org.gradle.cache.internal.DefaultCacheAccess@20ff28b
Starting Build
Settings evaluated using settings file '/Users/jenkins/home/ws/tankstream-android/tankstreamandroid/settings.gradle'.
Projects loaded. Root project using build file '/Users/jenkins/home/ws/tankstream-android/tankstreamandroid/build.gradle'.
Included projects: [root project 'tankstreamandroid', project ':app']
Evaluating root project 'tankstreamandroid' using build file '/Users/jenkins/home/ws/tankstream-android/tankstreamandroid/build.gradle'.
Creating new cache for metadata-2.23/module-metadata, path /Users/jenkins/.gradle/caches/modules-2/metadata-2.23/module-metadata.bin, access org.gradle.cache.internal.DefaultCacheAccess@1a84b028
Creating new cache for metadata-2.23/artifact-at-repository, path /Users/jenkins/.gradle/caches/modules-2/metadata-2.23/artifact-at-repository.bin, access org.gradle.cache.internal.DefaultCacheAccess@1a84b028
Evaluating project ':app' using build file '/Users/jenkins/home/ws/tankstream-android/tankstreamandroid/app/build.gradle'.
Using 4 worker leases.
Creating configuration compile
Creating configuration apk
Creating configuration provided
Creating configuration wearApp
Creating configuration annotationProcessor
Creating configuration jackPlugin
Creating configuration androidTestCompile
Creating configuration androidTestApk
Creating configuration androidTestProvided
Creating configuration androidTestWearApp
Creating configuration androidTestAnnotationProcessor
Creating configuration androidTestJackPlugin
Creating configuration testCompile
Creating configuration testApk
Creating configuration testProvided
Creating configuration testWearApp
Creating configuration testAnnotationProcessor
Creating configuration testJackPlugin
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory.  It is currently set to /Users/jenkins/home/android-sdk/ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.

Creating configuration debugCompile
Creating configuration debugApk
Creating configuration debugProvided
Creating configuration debugWearApp
Creating configuration debugAnnotationProcessor
Creating configuration debugJackPlugin
Creating configuration testDebugCompile
Creating configuration testDebugApk
Creating configuration testDebugProvided
Creating configuration testDebugWearApp
Creating configuration testDebugAnnotationProcessor
Creating configuration testDebugJackPlugin
Creating configuration releaseCompile
Creating configuration releaseApk
Creating configuration releaseProvided
Creating configuration releaseWearApp
Creating configuration releaseAnnotationProcessor
Creating configuration releaseJackPlugin
Creating configuration testReleaseCompile
Creating configuration testReleaseApk
Creating configuration testReleaseProvided
Creating configuration testReleaseWearApp
Creating configuration testReleaseAnnotationProcessor
Creating configuration testReleaseJackPlugin
Parsing the SDK, no caching allowed
SDK initialized in 3 ms
Creating new cache for metadata-2.23/artifact-at-url, path /Users/jenkins/.gradle/caches/modules-2/metadata-2.23/artifact-at-url.bin, access org.gradle.cache.internal.DefaultCacheAccess@1a84b028
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/android/support/appcompat-v7/25.3.1/appcompat-v7-25.3.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/android/support/appcompat-v7/25.3.1/appcompat-v7-25.3.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/android/support/constraint/constraint-layout/1.0.2/constraint-layout-1.0.2.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/android/support/constraint/constraint-layout/1.0.2/constraint-layout-1.0.2.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/android/support/design/25.3.1/design-25.3.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/android/support/design/25.3.1/design-25.3.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/android/support/support-v4/25.3.1/support-v4-25.3.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/android/support/support-v4/25.3.1/support-v4-25.3.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/google/android/gms/play-services-maps/11.0.1/play-services-maps-11.0.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/google/android/gms/play-services-maps/11.0.1/play-services-maps-11.0.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/google/android/gms/play-services-location/11.0.1/play-services-location-11.0.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/google/android/gms/play-services-location/11.0.1/play-services-location-11.0.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/google/android/gms/play-services-plus/11.0.1/play-services-plus-11.0.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/google/android/gms/play-services-plus/11.0.1/play-services-plus-11.0.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/google/android/gms/play-services-analytics/11.0.1/play-services-analytics-11.0.1.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/google/android/gms/play-services-analytics/11.0.1/play-services-analytics-11.0.1.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/android/support/cardview-v7/23.4.0/cardview-v7-23.4.0.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/android/support/cardview-v7/23.4.0/cardview-v7-23.4.0.jar]
Resource missing. [HTTP GET: http://jcenter.bintray.com/com/android/support/customtabs/23.4.0/customtabs-23.4.0.pom]
Resource missing. [HTTP HEAD: http://jcenter.bintray.com/com/android/support/customtabs/23.4.0/customtabs-23.4.0.jar]
Creating new cache for metadata-2.23/module-artifacts, path /Users/jenkins/.gradle/caches/modules-2/metadata-2.23/module-artifacts.bin, access org.gradle.cache.internal.DefaultCacheAccess@1a84b028
Parsing /Users/jenkins/home/android-sdk/build-tools/25.0.2/package.xml
Parsing /Users/jenkins/home/android-sdk/build-tools/26.0.1/package.xml
Parsing /Users/jenkins/home/android-sdk/build-tools/26.0.2/package.xml
Parsing /Users/jenkins/home/android-sdk/emulator/package.xml
Parsing /Users/jenkins/home/android-sdk/patcher/v4/package.xml
Parsing /Users/jenkins/home/android-sdk/platform-tools/package.xml
Parsing /Users/jenkins/home/android-sdk/platforms/android-25/package.xml
Parsing /Users/jenkins/home/android-sdk/platforms/android-26/package.xml
Parsing /Users/jenkins/home/android-sdk/tools/package.xml
java.net.ConnectException: Connection refused (Connection refused)
IOException: https://dl.google.com/android/repository/addons_list-3.xml
java.net.ConnectException: Connection refused (Connection refused)
IOException: https://dl.google.com/android/repository/addons_list-2.xml
java.net.ConnectException: Connection refused (Connection refused)
IOException: https://dl.google.com/android/repository/addons_list-1.xml
java.net.ConnectException: Connection refused (Connection refused)
Failed to download any source lists!

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> java.lang.NullPointerException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Total time: 10.186 secs
Stopped 0 worker daemon(s).
Received result Failure[value=org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: A problem occurred configuring project ':app'.] from daemon DaemonInfo{pid=6465, address=[3646567b-c480-4beb-a6d2-2ba0120d1cd8 port:54872, addresses:[/0:0:0:0:0:0:0:1, /127.0.0.1]], state=Idle, lastBusy=1506668208517, context=DefaultDaemonContext[uid=6c85c027-6030-4dfe-91b8-6a40166e80af,javaHome=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home,daemonRegistryDir=/Users/jenkins/.gradle/daemon,pid=6465,idleTimeout=10800000,daemonOpts=-XX:MaxPermSize=256m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx1024m,-Dfile.encoding=UTF-8,-Duser.country=AU,-Duser.language=en,-Duser.variant]} (build should be done).