每当我尝试在两个表之间创建一对多的关系时,我意识到只有在表一中的主键与表2中不是主键的元素之间存在关系时才会发挥作用.如果它有主键,则它会自动更改为一对一关系
有一个名为"InStrRev"的函数在Access中工作正常,但是当我使用相同的函数来获取C#窗体中的记录时,会弹出一条错误消息说
表达式中的未识别函数'InStrRev'.
有没有办法可以使用这个函数,还是我可以在我的Access查询中使用一些其他函数来获取字段中任何字符的最后一个索引?
我在从VB.NET创建Access表时遇到问题.
这是我提出的代码,但我不断收到错误:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
'connection string
Dim dbpath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
dbpath = New Uri(dbpath).LocalPath
Dim my_connection As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\GhostDrive\Desktop\database.mdb"
Dim userTables As DataTable = Nothing
Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
Dim source As String
'query string
Dim my_query As String = "CREATE TABLE " & TextBox2.Text & " ( " & _
"ID Counter, " & _
"Year datetime," & _
"Title varchar(40)," & _ …Run Code Online (Sandbox Code Playgroud) 我有两张表:
和
我希望两个创建一个表,其中对于每个 CustomerID,每个 Product_Interest 连接到最接近日期(但不是之后)的 Lead_Source。决赛桌将是:
到目前为止,我可以连接表,并创建一个新字段来计算最接近的日期而无需遍历,但是当我尝试使用 Min 进行分组时,我仍然得到多个排列(每个 Lead_Date 到每个 Product_Interest)。这是代码:
SELECT Min(Int(Abs([Test_PI]![Product_Interest_Date]-[Test_Leads]![Lead_Date])))
AS Lead_PI_Link,
Test_Leads.CustomerID,
Test_PI.Product_Interest_Date,
Test_PI.Product_Interest,
Test_Leads.Lead_Date,
Test_Leads.Lead_Source
FROM Test_Leads INNER JOIN Test_PI ON Test_Leads.CustomerID = Test_PI.CustomerID
GROUP BY Test_Leads.CustomerID,
Test_PI.Product_Interest_Date,
Test_PI.Product_Interest,
Test_Leads.Lead_Date,
Test_Leads.Lead_Source
HAVING (((Test_Leads.CustomerID)="C6UJ9A002Q2P"));
Run Code Online (Sandbox Code Playgroud)
此 CustomerID 在 Test_Leads 中有 4 个条目,在 Product_Interest 中有 4 个条目。此查询的结果给出 16 个结果,而不是所需的 4 个结果。如果日期完全匹配,我可以添加一个条件,即日期差异为“0”,但是,有时这些日期会偏移 1 天,有时会偏移 1 天。很多天。
我正在使用 Access,并且更喜欢“本机”解决方案,但此时我已经做好了一切准备!
我正在使用连接器/Python 将多行插入到 mysql 的临时表中。这些行都在一个列表列表中。我像这样执行插入:
cursor = connection.cursor();
batch = [[1, 'foo', 'bar'],[2, 'xyz', 'baz']]
cursor.executemany('INSERT INTO temp VALUES(?, ?, ?)', batch)
connection.commit()
Run Code Online (Sandbox Code Playgroud)
我注意到(当然还有更多的行)性能非常差。使用 SHOW PROCESSLIST,我注意到每个插入都是单独执行的。但是文档https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html说这应该优化为 1 个插入。这是怎么回事?
我正在尝试将以下列表传递给sql查询
x = ['1000000000164774783','1000000000253252111']
Run Code Online (Sandbox Code Playgroud)
我正在使用sqlalchemy和pyodbc连接到sql:
import pandas as pd
from pandas import Series,DataFrame
import pyodbc
import sqlalchemy
cnx=sqlalchemy.create_engine("mssql+pyodbc://Omnius:MainBrain1@172.31.163.135:1433/Basis?driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0")
Run Code Online (Sandbox Code Playgroud)
我尝试使用各种字符串格式函数在sql查询中使用。下面是其中之一
xx = ', '.join(x)
sql = "select * from Pretty_Txns where Send_Customer in (%s)" % xx
df = pd.read_sql(sql,cnx)
Run Code Online (Sandbox Code Playgroud)
他们似乎都将其转换为数字格式
(1000000000164774783, 1000000000253252111) instead of ('1000000000164774783','1000000000253252111')
Run Code Online (Sandbox Code Playgroud)
因此查询失败,因为SQL 中Send_Customer的数据类型为varchar(50)
ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0]
[SQL Server]Error converting data type varchar to numeric. (8114) (SQLExecDirectW)')
[SQL: 'select * from Pretty_Txns where Send_Customer in (1000000000164774783, 1000000000253252111)']
Run Code Online (Sandbox Code Playgroud) 嗨,我目前在Python 3中使用pyodbc,并且试图通过无需在不同计算机上进行手动更改而自动检测ODBC驱动程序的方法。原因是因为我的计算机具有ODBC驱动程序13,而另一位朋友的计算机具有ODBC驱动程序11,因此无论何时从侧面运行脚本,都必须先手动更改版本才能执行该过程。
谁能帮助解决这个问题?以下是我的示例代码。
谢谢
import os
import csv
import pyodbc
import datetime
from dateutil.relativedelta import relativedelta
conn = pyodbc.connect(
r'DRIVER={ODBC Driver 13 for SQL Server};'
r'SERVER=****;'
r'DATABASE=****;'
r'Trusted_Connection=yes;'
)
cursor = conn.cursor()
cursor.execute("Select * From Table1")
dData = cursor.fetchall()
Run Code Online (Sandbox Code Playgroud) 我目前正在使用python实现下面的简单查询,使用pyodbc在SQL服务器表中插入数据:
import pyodbc
table_name = 'my_table'
insert_values = [(1,2,3),(2,2,4),(3,4,5)]
cnxn = pyodbc.connect(...)
cursor = cnxn.cursor()
cursor.execute(
' '.join([
'insert into',
table_name,
'values',
','.join(
[str(i) for i in insert_values]
)
])
)
cursor.commit()
Run Code Online (Sandbox Code Playgroud)
只要没有重复键(假设第一列包含键),这应该可以工作.但是对于具有重复键的数据(表中已存在数据),将引发错误.如何使用pyodbc一次性在SQL服务器表中插入多行,以便只更新具有重复键的数据.
注意:针对单行数据提出了解决方案,但是,我想一次插入多行(避免循环)!
I have a SpringBoot app, where I use jdbcTemplate to insert a row to a mssql
int numOfRowsAffected = remoteJdbcTemplate.update("insert into dbo.[ELCOR Resource Time Registr_] "
+ "( [Entry No_], [Record ID], [Posting Date], [Resource No_], [Job No_], [Work Type], [Quantity], [Unit of Measure], [Description], [Company Name], [Created Date-Time], [Status] ) "
+ " VALUES (?,CONVERT(varbinary,?),?,?,?,?,?,?,?,?,?,?);",
ELCORResourceTimeRegistr.getEntryNo(),
ELCORResourceTimeRegistr.getEntryNo()),
ELCORResourceTimeRegistr.getPostingDate(),
ELCORResourceTimeRegistr.getResourceNo(),
jobNo,
ELCORResourceTimeRegistr.getWorkType(),
ELCORResourceTimeRegistr.getQuantity(),
ELCORResourceTimeRegistr.getUnitOfMeasure(),
ELCORResourceTimeRegistr.getDescription(),
ELCORResourceTimeRegistr.getCompanyName(),
ELCORResourceTimeRegistr.getCreatedDate(),
0);
Run Code Online (Sandbox Code Playgroud)
the value of ELCORResourceTimeRegistr.getEntryNo() is a String with the value 0x00173672
but …
我知道Pypyodbc是Pyodbc的较新版本。之前有人问过这两个之间的比较问题,但我想将Pymssql添加到混合中。
python ×5
sql-server ×5
ms-access ×4
pyodbc ×4
c# ×1
connector ×1
executemany ×1
jdbc ×1
jdbctemplate ×1
join ×1
mssql-jdbc ×1
mysql ×1
odbc ×1
pymssql ×1
pypyodbc ×1
python-3.x ×1
relationship ×1
spring-jdbc ×1
sql ×1
sqlalchemy ×1
vb.net ×1
winforms ×1