# SQLite Error: UNIQUE constraint failed

**URL:** https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758
**Category:** General
**Created:** [6 April 2021 11:21 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758 "2021-04-06T11:21:17Z")
**Posts on this page:** 15
**Page:** 2

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [6 April 2021 15:49 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/21 "2021-04-06T15:49:10Z")

</div>

> [@Samira05](#):
>
> how many database have i ?

I think it would do you a great deal of good to take an sqlite tutorial. That way you could learn how to answer your question yourself.

This forum supports Node-RED and is not a "Teach me how to use Sqlite or MySQL or how do I code JavaScript. there are plenty of places to learn those basics.

In NR you define the SQLITE `database` when you configure the `sqlite` node. You can add as many `tables` as you want to one database. So, off hand, I can't think of a way to tell how many databases you have created except to look at all the `sqlite` nodes you have in your flows.

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [6 April 2021 16:01 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/22 "2021-04-06T16:01:44Z")

</div>

Thank you zenofmud for your help.  
You are right , i can ask the question about NR here. I create database and table in NR according to 2 tutorials. one was with random function and i could do it correctly. in my project with csv file i did to create DB as well as those tutorials but i got errors. I can not understand how works my random projekt and DB of it but my new DB plays just with me. i am confused for this reson i wrote my question here.  
tnx a lot an have a nice day.

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [13 April 2021 09:00 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/23 "2021-04-13T09:00:02Z")

</div>

Hi everyone,  
last problem is solved and it is worked correct till today. I found today that my data in database are not sequential like my csv file. why is it happend? Data transfer sequential but when i see output von DB with debug node , i am counfused because of that.  
Is that so, or i have mistake in my function?

 ![csvfile](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7fb4c01e105c121942ee59a0a42de0ae0a9fd65.png)  
 ![csvfile in DB](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21a896a8cab1a66c95b4ef86cf7a85e5f9714969.png)

Best Regards  
Samira

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 April 2021 10:02 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/24 "2021-04-13T10:02:39Z")

</div>

Records in a database are not in any particular order. When you query it you can tell it to give you the data in whatever order you want, so if you have a time column then you can add `sort by time` to the query to tell it to give you the data in that order. Make sure that you have an index on that column in the db or the sort will be very slow.

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [13 April 2021 10:22 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/25 "2021-04-13T10:22:36Z")

</div>

Thank you for your replay. it is correct and i can do this but i have every 24 hours temperature from a device.This data save in csv file for example since 03-11-2020 01:00:00 till next day 04-11-2020 00:45:00 , therfore i cannot select time as query and temperature too. How can i play with date and time and select them as increse oder decrese of order?  
Tnx  
Sam

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 April 2021 10:26 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/26 "2021-04-13T10:26:52Z")

</div>

What are the columns in the database and what types are they?

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [13 April 2021 10:36 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/27 "2021-04-13T10:36:08Z")

</div>

error TEXT NOT NULL (it is '0' in every rows)  
UTCOffsetMin TEXT NOT NULL(it is '60'in every rows)  
DateTime TEXT NOT NULL,  
Temperature FLOAT NOT NULL

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 April 2021 11:02 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/28 "2021-04-13T11:02:04Z")

</div>

That looks ok

> [@Samira05](#):
>
> therfore i cannot select time as query and temperature too

I don't understand what you mean. Why can't you select the columns and sort by time?  
What query are you using to get the data?

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [13 April 2021 11:10 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/29 "2021-04-13T11:10:18Z")

</div>

because of DateTime column. my time increse till 11:45:00 then decrese on 00:00:00 and 00:15:00, 00:30:00 and 00:45:00.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 April 2021 11:12 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/30 "2021-04-13T11:12:24Z")

</div>

> [@Samira05](#):
>
> my time increse till 11:45:00 then decrese on 00:00:00 and 00:15:00, 00:30:00 and 00:45:00.

Sorry, I have no idea what you mean.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [13 April 2021 11:26 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/31 "2021-04-13T11:26:25Z")

</div>

You should be storing date/time values as integers which are UNIX timestamps. Then you can easily work with them in Node-RED (a JavaScript timestamp is 1000x a UNIX timestamp). SQLite doesn't have a proper date/time field type but all of its date/time functions will work with 3 different formats of which integer is best for working with Node-RED.

[Datatypes In SQLite Version 3](https://www.sqlite.org/datatype3.html)

This page of the manual shows you how to work with date/time values in SQLite

[Date And Time Functions (sqlite.org)](https://www.sqlite.org/lang_datefunc.html)

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [13 April 2021 11:38 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/32 "2021-04-13T11:38:37Z")

</div>

> [@Samira05](#):
>
> This data save in csv file for example since 03-11-2020 01:00:00 till next day 04-11-2020 00:45:00 ,

Thank you for your response. I try it .

Best regards  
Sam

---

<div class="post-metadata">

### Author: ![Samira05](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Samira05](https://discourse.nodered.org/u/Samira05)
#### Post date: [13 April 2021 12:13 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/33 "2021-04-13T12:13:21Z")

</div>

I have another question. I recieve csv. file from some devices every day. I want to save data automatically to database but i have not idea how i can do it. i thank you for your advice.

Bst Regards  
Sam

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [13 April 2021 12:23 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/34 "2021-04-13T12:23:34Z")

</div>

[SQLite - How to import a CSV file into SQLite table? | TablePlus](https://tableplus.com/blog/2018/07/sqlite-how-to-import-csv-file-into-sqlite-table.html)

Google.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [12 June 2021 12:24 UTC](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758/35 "2021-06-12T12:24:09Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/sqlite-error-unique-constraint-failed/43758.md?page=1)
