ID undefined
(1) By Piero (Pierre) on 2022-08-01 08:38:12 [link] [source]
I'm developing an app with react native and SQLite to schedule daily events, but when I insert an event the id returns undefined.
In my database.js I have this:
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
date TEXT NOT NULL,
hour TEXT NOT NULL
)
,
(2) By MBL (UserMBL) on 2022-08-01 10:29:43 in reply to 1 [source]
when I insert an event the id returns undefined
How does your insert statement look like? How is your id "returned"?
What do you mean by "undefined" ?
Which language you are developing in? Which wrapper do you use? (Or the native C-API ?)
What is your question?
(3) By Piero (Pierre) on 2022-08-01 10:34:33 in reply to 2 [link] [source]
export function insertEvent(event) {
const promise = new Promise((resolve, reject) => {
database.transaction((tx) => {
tx.executeSql(
INSERT INTO events (title, description,date, hour) VALUES (?, ?, ?, ?)
,
[
event.title,
event.description,
// Set up date
event.date.toLocaleDateString(),
// Set up hour with italian fuse format
event.hour.toLocaleTimeString('it-IT', {hour: '2-digit', minute: '2-digit'})
],
(, result) => {
resolve(result);
},
(, error) => {
r
reject(error);
}
);
});
});
return promise;
}
React Native with expo
(4) By Ryan Smith (cuz) on 2022-08-01 10:41:27 in reply to 1 [link] [source]
Not sure what you mean, but:
- INSERT statements do not return IDs (unless you're using the new RETURNING clause?).
- Your "id" PK field cannot possibly ever be NULL or undefined.
Do you maybe mean you are querying the last_insert_rowid and it returns undefined? If so, how are you doing it? via the JS wrapper? in SQL? Do you do it from the same connection? Directly after inserting?
Please show the full SQL and/or code you use to do this, along with what you expect to see and what you actually see, else we're just guessing.
(5) By Piero (Pierre) on 2022-08-01 10:58:08 in reply to 4 [link] [source]
It returns an event in this mode: Event { "date": "1/8/2022", "description": "Test", "hour": "13:00", "id": undefined, "title": "Test", }
Select function:
export function getEvents() {
const promise = new Promise((resolve, reject) => {
database.transaction((tx) => {
tx.executeSql(
SELECT * FROM events
,
[],
(, result) => {
const dbEvent = result.rows._array.map((row) => {
return new Event(row.title, row.description, row.date, row.hour);
});
console.log(dbEvent);
resolve(dbEvent);
},
(, error) => {
reject(error);
}
);
});
});
return promise;
}
(6) By Ryan Smith (cuz) on 2022-08-01 12:53:51 in reply to 5 [link] [source]
Not 100% sure, and this is a JS question, not really an SQLite question, but from the looks of it your Event doesn't set the id at all.
i.e. this line:
const dbEvent = result.rows._array.map((row) => {
return new Event(row.title, row.description, row.date, row.hour);
});
should it not be:
const dbEvent = result.rows._array.map((row) => {
return new Event(row.id, row.title, row.description, row.date, row.hour);
});
?
(7) By Piero (Pierre) on 2022-08-01 13:03:49 in reply to 6 [link] [source]
Thank you very much, it now functions.
(8) By Gunter Hick (gunter_hick) on 2022-08-01 13:11:43 in reply to 5 [link] [source]
What Ryan said. Plus, you probably don't need AUTOINCREMENT; the INTEGER PRIMARY KEY invocation will already automatically allocate rowids (usually 1 more than the last one inserted). AUTOINCREMENT will prevent SQLite from re-using rowids "forever". See https://sqlite.org/autoinc.html