如何动态添加列到sqlite数据库? [英] How to dynamically add columns to sqlite database?

查看:485
本文介绍了如何动态添加列到sqlite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用桌面应用程序。



因此前端有不同的按钮,点击该按钮可从连接到PC的硬件获取信息通过串口并将数据存储到数据库中并显示信息。



问题是,如果我在我的数据库中创建一个包含18列的表,并假设信息提取需要19列然后我将在运行时添加1列。



如果需要更少的列,则相同。



我尝试过的事情:



1.我读过有关EAV但我不理解的事。



2.我正在考虑获取所需的最大列数,只显示或不显示在前端。

I am working on a desktop application.

So there are different buttons on the front end and a click on that button fetch the information from hardware connected to PC via Serial Port and store that data into database and shows the information.

Problem is that if i create a table in my database with 18 columns and suppose the information fetch requires 19 columns then i will have to add 1 column at run time.

same if less no of columns are required.

What I have tried:

1.I read about EAV but I didn't understand it.

2.I was thinking about taking maximum numbers of columns required and just showing or not showing them at the front end.

推荐答案

您可以添加列,但它很复杂 - 不在实际代码中,它是SQL ALTER TABLE命令: SQ L ALTER TABLE语句 [ ^ ] - 问题在于表格中的现有数据。

如果您有18列,并且添加了19,那么现有行的列应该是什么值?您可以为新表提供NULL属性,在这种情况下,现有行将获得NULL值,但如果您这样做,您仍需要在以后的演示文稿软件中处理这些空值。并且......如果你的表有大量的行,这可能是非常低效的,因为SQL必须创建一个全新的表并在删除旧表之前将现有数据复制到其中。



我个人的感觉是你做错了什么:我会被问到为什么这个设备给我一个不同的行数?,当行数改变时,行订单?,这是否意味着现有数据不再在正确的列中?,如果设备决定向我发送较少的列,会发生什么?

所有这些都可能具有深刻意义以后会对您的代码可靠性和数据完整性产生影响 - 一旦您遇到数据完整性问题,它们就是解决问题的噩梦!
You can add columns, but it's complicated - not in the actual code, it's an SQL ALTER TABLE command: SQL ALTER TABLE Statement[^] - the problem is with existing data in the table.
If you have 18 columns, and you add a 19th, what value should go into that column for existing rows? You could give the new table the NULL attribute, in which case existing rows will get NULL values, but if you do that you will still have to deal with those nulls in your presentation software later. And ... if your table has a significant number of rows this could be extremely inefficient, as SQL has to create a whole new table and copy existing data into it before removing the old table.

My personal feeling is that you are doing something wrong: I'd be included to ask myself "why is this device giving me a different number of rows?", "when the row count changes, does the row order?", "will this mean that existing data is no longer in the right column?", and "what happens if the device decides to send me less columns?"
All of these could have profound results on your code reliability and data integrity later - and once you get data integrity problems, they are a nightmare to fix!


这篇关于如何动态添加列到sqlite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆