嗨,我正在做一个抢劫项目。我是这个新手,也是python新手。我有一个python代码,可从光传感器读取灯具的照度。我想获取这些勒克斯水平读数并将其保存到Sqlite数据库表中。我阅读勒克斯的代码在下面
library(ggplot2)
library(shiny)
data1 <- mtcars
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "xaxis",
label = "Choose a Variable for the X-axis of the First Graph",
choices = colnames(data1)
),
selectInput(
inputId = "yaxis",
label = "Choose a Variable for the Y-axis of the First Graph",
choices = colnames(data1)
)
),
mainPanel(
plotOutput(outputId = "scatterplot"))
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
req(input$xaxis)
req(input$yaxis)
ggplot(data1, aes(x = input$xaxis, y = input$yaxis))+geom_point()
})}
shinyApp(ui = ui, server = server)
我想将亮度值保存到Sqlite数据库表中
答案 0 :(得分:0)
我建议在Python中遵循有关sqllite3的本教程:
https://www.pythoncentral.io/introduction-to-sqlite-in-python/
一旦您阅读并尝试了它,就可以再次开始处理您的特定问题:
首先,您需要通过创建将要填充数据的表来初始化数据库。要创建数据库,可以通过创建“模式”并在模式中执行代码来完成。一个示例架构可能看起来像这样:
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS post;
CREATE TABLE user(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE post(
id INTEGER PRIMARY KEY AUTOINCREMENT,
author_id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY(author_id) REFERENCES user(id)
);
(显然,您的表与用于保存光传感器数据的表将不同。根据您要存储的内容,您甚至可能只需要一张表)
注意:您可能需要研究类似Python的click模块(它允许您创建与python代码相关的命令行命令)之类的东西,以便可以独立于运行的程序来初始化数据库。另外,您可以在程序中添加一些代码来检查其是否已经存在,并仅在不存在时对其进行初始化
然后最后一步就是使用sqllite代码将数据存储在表中(请参阅:先前链接的教程)