[英]AWS Lambda function can't connect to and query SQLite DB file from S3
My goal is to upload a SQLite database file to AWS S3, use AWS Lambda & python ( sqlite3
) to connect to the database, query it, and return some of its data.我的目标是将 SQLite 数据库文件上传到 AWS S3,使用 AWS Lambda & python (
sqlite3
) 连接到数据库,查询它,并返回它的一些数据。
Having uploaded the database file to S3, I wrote a python script (which is to become the Lambda function) that successfully downloads the database from S3, connects to it with sqlite3
, and returns some query results.将数据库文件上传到 S3 后,我编写了一个 python 脚本(将成为 Lambda 函数)成功地从 S3 下载数据库,使用
sqlite3
连接到它,并返回一些查询结果。
The issue is that when I take the exact same code and put it in AWS Lambda, I get the following error:问题是,当我采用完全相同的代码并将其放入 AWS Lambda 时,出现以下错误:
{
"errorMessage": "malformed database schema (message_idx_undelivered_one_to_one_imessage) - near \"where\": syntax error",
"errorType": "DatabaseError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n print(cursor.execute(\"select mycolumn from message limit 1\").fetchone())\n"
]
}
Here is the python script/Lambda function:这是 python 脚本/Lambda function:
import sqlite3
import boto3
def lambda_handler(event, context):
s3 = boto3.resource("s3")
bucket = s3.Bucket("my-bucket")
bucket.download_file("my_database.db", "/tmp/my_database.db")
conn = sqlite3.connect("/tmp/my_database.db")
cursor = conn.cursor()
print(cursor.execute("select mycolumn from message limit 1").fetchone())
Running this file locally works correctly.在本地运行此文件可以正常工作。 I have confirmed that both my local environment and Lambda are using these versions of the following:
我已经确认我的本地环境和 Lambda 都在使用以下这些版本:
boto3: 1.20.32
python: 3.9.13
sqlite3: 2.6.0
Here is the schema of the database I'm trying to read.这是我正在尝试读取的数据库的架构。
Why is Lambda producing this error while my local environment isn't?为什么 Lambda 会产生此错误,而我的本地环境却不会? How can I go about getting python to connect to the database within the Lambda function?
请问go如何获取python连接到Lambda function内的数据库?
From pastebin extract here is your DDL in your database schema which triggers your error when opening your database从 pastebin 提取这里是你的数据库模式中的 DDL,它在打开你的数据库时触发你的错误
CREATE INDEX message_idx_undelivered_one_to_one_imessage
ON message(
cache_roomnames,
service,
is_sent,
is_delivered,
was_downgraded,
item_type
) where cache_roomnames IS NULL
AND service = 'iMessage'
AND is_sent = 1
AND is_delivered = 0
AND was_downgraded = 0
AND item_type == 0;
The partial index feature has been integrated in sqlite since the 3.8.0 version https://www.sqlite.org/partialindex.html . sqlite自3.8.0版本https://www.sqlite.org/partialindex.html开始集成了部分索引功能。 The sqlite3 version on the lambda environment is probably to old to handle such king of schema.
lambda 环境中的 sqlite3 版本可能太旧,无法处理这种模式之王。 An version bump in your sqlite3 dependency in your lambda environment should fix your issue.
lambda 环境中的 sqlite3 依赖项中的版本提升应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.