Create database row once

Im having a problem on my rows, currently whenever I run my app, it creates a new row, i just wanted it to be single rowed and will not increment for I will use it for updates in my app.

I removed your duplicate post.  Please only ask your questions once.

Thanks

Rob

Okay sorry, well at least you could have posted some advice :slight_smile:

I’m not an SQLite expert, I was hoping that the community would respond to you.

The question is not very clear to me. You have insert query somewhere that is run every time you run the app?

You can check if you have this row already with a select statement and see if it returns anything.

basically whenever I ran my app it keeps on creating a new row on my database, where all I wanted is it will add a new row one time only unless the user clears the data or uninstalls the app. since I can’t put predefined database on my documents directory, I have to manually create it using corona. upon opening the app, It will create a database if it does not exist so as a table, and it will fill up a new row. The problem is whenever I rerun the app(close the app->open it again) it creates a new row. making duplicate data. I just wanted to stop creating a new row, once it has found a row inside of it

You can check if the database file exists like this:

[lua]local dbFileName = “db.sqlite”

local path       = system.pathForFile( dbFileName, system.DocumentsDirectory )

local fhd        = io.open( path )

– Determine if file exists

if fhd then

    – File exists

    io.close(fhd)

end

[/lua]

Okay Got it thanks :slight_smile:

BTW are there anyway to identify if any row doesn’t exist?

example

ID  |   NAME     |

1   |   asdasd    |

I want to retrieve row 2 since its empty my idea is it should be like this

for row in db:exec([[SELECT \* FROM Table WHERE ID = '2']]) if row.id == nil then --Display Something to represent ID 2 onwards are empty else --Display Something end end

the problem is, When I view it in the corona terminal it doesn’t return something to me, not even a nil, so the 1st condition is bypassed

This is how I do it

[lua]function idExists(id) 

    

    local query = “SELECT id FROM someTableName WHERE id = " …  id … " LIMIT 1;”

    

    for row in db:nrows(query) do

        return true

    end

    

    return false

    

end[/lua]

To test things outside of Corona I recommend getting some SQLite manager program. I use Firefox with this plugin: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

Then you open your SQLite database from the Corona documents directory.

Whenever something is not going as expected, print out the query in Corona and paste it into the SQLite manager and test it out. There you will get a better error message so you can perfect your query. That will make life a lot easier IMO.

Yes I’ve been doing that, it just not identifies empty fields as null/nil, that’s why my 1st condition is bypassed

I removed your duplicate post.  Please only ask your questions once.

Thanks

Rob

Okay sorry, well at least you could have posted some advice :slight_smile:

I’m not an SQLite expert, I was hoping that the community would respond to you.

The question is not very clear to me. You have insert query somewhere that is run every time you run the app?

You can check if you have this row already with a select statement and see if it returns anything.

basically whenever I ran my app it keeps on creating a new row on my database, where all I wanted is it will add a new row one time only unless the user clears the data or uninstalls the app. since I can’t put predefined database on my documents directory, I have to manually create it using corona. upon opening the app, It will create a database if it does not exist so as a table, and it will fill up a new row. The problem is whenever I rerun the app(close the app->open it again) it creates a new row. making duplicate data. I just wanted to stop creating a new row, once it has found a row inside of it

You can check if the database file exists like this:

[lua]local dbFileName = “db.sqlite”

local path       = system.pathForFile( dbFileName, system.DocumentsDirectory )

local fhd        = io.open( path )

– Determine if file exists

if fhd then

    – File exists

    io.close(fhd)

end

[/lua]

Okay Got it thanks :slight_smile:

BTW are there anyway to identify if any row doesn’t exist?

example

ID  |   NAME     |

1   |   asdasd    |

I want to retrieve row 2 since its empty my idea is it should be like this

for row in db:exec([[SELECT \* FROM Table WHERE ID = '2']]) if row.id == nil then --Display Something to represent ID 2 onwards are empty else --Display Something end end

the problem is, When I view it in the corona terminal it doesn’t return something to me, not even a nil, so the 1st condition is bypassed

This is how I do it

[lua]function idExists(id) 

    

    local query = “SELECT id FROM someTableName WHERE id = " …  id … " LIMIT 1;”

    

    for row in db:nrows(query) do

        return true

    end

    

    return false

    

end[/lua]

To test things outside of Corona I recommend getting some SQLite manager program. I use Firefox with this plugin: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

Then you open your SQLite database from the Corona documents directory.

Whenever something is not going as expected, print out the query in Corona and paste it into the SQLite manager and test it out. There you will get a better error message so you can perfect your query. That will make life a lot easier IMO.

Yes I’ve been doing that, it just not identifies empty fields as null/nil, that’s why my 1st condition is bypassed