简体   繁体   English

Haskell,HDBC.Sqlite3-如果尚不存在该如何添加列?

[英]Haskell, HDBC.Sqlite3 - How to add a column if it doesn't exist already?

I have a function that given an Int returns a list of lists of Strings. 我有一个函数,给定一个Int返回字符串列表的列表。

fetchParts :: Int -> [[String]]

This is what the output looks like 这就是输出的样子

[["title", "some title"], ["rate", "2.4"], ["dist", "some string"], ["tr", "1"], ["td, "2"] ..]]

The length of the output can be variable. 输出的长度可以是可变的。 Only the first 3 lists can be present 100% of the time. 仅前3个列表可以100%的时间显示。

The later part of the list can be 列表的后半部分可以是

["a", "1"], ["b", "2"] ..

or 要么

["some", "1"], ["part", "2"], ["of", "3"] ..]

or 要么

["ex1", "a"], ["ex2", "b"], ..]

or some other combination of strings. 或其他字符串组合。

And I want to add this output to a sqlite3 database file. 我想将此输出添加到sqlite3数据库文件中。 I'm using HDBC and HDBC.Sqlite3 for this. 我使用的是华夏邓白氏中国HDBC.Sqlite3这一点。

To add something to a database file I'm running functions like these 要将某些内容添加到数据库文件中,我正在运行以下功能

  initialConnection <- connectSqlite3 "src/parts.db"
  run initialConnection partsEntry []
  commit initialConnection
  disconnect initialConnection

where partsEntry is a simple SQL String like this 其中partsEntry是像这样的简单SQL字符串

partsEntry = "INSERT INTO PARTSDATA ( title, rate, dist, ...) VALUES ( "some title", "2.4", "some string", ...)

where 哪里

( title, rate, dist, ...) are from head <$> fetchParts 1 ( title, rate, dist, ...)来自head <$> fetchParts 1

and

("some title", "2.4", "some string" ...) are from last <$> fetchParts 1 ("some title", "2.4", "some string" ...)来自last <$> fetchParts 1

The problem is say if "some" column doesn't exists, code will throw errors. 问题是,如果"some"列不存在,则代码将引发错误。

What I want to do is something like this 我想做的是这样的

  • if column "abc" doesn't exists, add column "abc" and insert "this" value at the current row 如果"abc"列不存在,则添加"abc"列并在当前行插入"this"
  • if column "abc" exists, just insert "this" value at the current row 如果存在"abc"列,只需在当前行插入"this"

But I'm not sure how to go about doing that. 但是我不确定该怎么做。

I was able to solve the problem. 我能够解决问题。

First use describeTable function from HDBC package. 首先使用HDBC软件包中的describeTable函数。 The function will return column names and type. 该函数将返回列名称和类型。 If you just need the names like I did, this is what you can do 如果您只需要像我一样的名字,这就是您可以做的

getColumnsInTable :: conn -> String -> IO [String]
getColumnsInTable conn tableName = do
  d <- describeTable conn tableName
  return $ fst <$> d

The return will have all the columns' names. 返回将具有所有列的名称。

Scan through the list to see if it contains all the columns you wish. 浏览列表以查看其中是否包含您想要的所有列。 If it doesn't use a function like the following to alter the table, ie add a new column with INT type. 如果不使用以下函数来更改表,即添加具有INT类型的新列。

createNewColumn conn columnName = do
  let stmt = "ALTER TABLE FantasyBooks ADD COLUMN " ++ columnName ++ " INT;"
  run conn stmt []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM