INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

SQLite update syntax

SQLite update syntax

(OP)
I have a code in C# that should update a column form my table. Table is `accounts`, row is `totalsimyes` and new value that should be inserted in column is `totalsimda`.

CODE -->

string updatecorect = "select totalsimyes from accounts where username = 'DPaul'";
                                SQLiteCommand updatec = new SQLiteCommand(updatecorect, Conexiune.getConnection());
                                SQLiteDataReader upd = updatec.ExecuteReader();
                                while(upd.Read())
                                {
                                        int totalsimda = (Int32)upd["totalsimyes"];
                                        totalsimda++;
                                        using (SQLiteCommand update = new SQLiteCommand("update accounts set totalsimyes=@totalsimda where username='DPaul'", Conexiune.getConnection()))
                                        {
                                            update.Parameters.AddWithValue("@totalsimda", totalsimda);
                                            int rows = update.ExecuteNonQuery();
                                        }
                                } 

I don't receive any errors, but when I get in this code at runtime, it just locks and I get do anything.

RE: SQLite update syntax

Hi there,

to me, this seems overly complicated. You read from your table and try to update the record you currently have an open connection to, which probably locks the record.
Of course you could try fixing this by setting the reader connection options to read-only.
However, it also seems that all you want to do is increment totalsimyes by 1 for all records where username='DPaul'. Is that assumption correct?
Have you tried simply using the sql update command directly and executing it using SqlCommand ExecuteNonReader method?
Example:

CODE

SQLiteConnection Conexiune=new SQLiteConnection("Data Source=C:\\00_Projekte_temp\\sqlite\\test.sqlite;Version=3;");
string updatecorect = "update accounts set totalsimyes=totalsimyes+1 where username = 'DPaul'";
SQLiteCommand updatec = new SQLiteCommand(updatecorect, Conexiune);
updatec.ExecuteNonQuery(); 

Cheers,
MakeItSo

ôKnowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.ö (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: SQLite update syntax

(OP)
I tried this solution right now and it works just the same.

RE: SQLite update syntax

Please post the exact code portion you used.
Did you replace the loop and DataReader portion with my suggestion?
What do you mean with "lock"?

ôKnowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.ö (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: SQLite update syntax

(OP)
I find out where is the problem, but I don't really know how to solve it. That is the code. THe problem is dat `database is locked`. Which means, when I open that select, I can't use that update because i already have an opened select. But I;m looking for a solution without using oepn() and close() for connections cause I use a class for connection which I use for every query (Conexiune.getConnection())

RE: SQLite update syntax

Quote:

when I open that select, I can't use that update
That's what I meant.
Replace this whole shebang:

CODE

string updatecorect = "select totalsimyes from accounts where username = 'DPaul'";
                                SQLiteCommand updatec = new SQLiteCommand(updatecorect, Conexiune.getConnection());
                                SQLiteDataReader upd = updatec.ExecuteReader();
                                while(upd.Read())
                                {
                                        int totalsimda = (Int32)upd["totalsimyes"];
                                        totalsimda++;
                                        using (SQLiteCommand update = new SQLiteCommand("update accounts set totalsimyes=@totalsimda where username='DPaul'", Conexiune.getConnection()))
                                        {
                                            update.Parameters.AddWithValue("@totalsimda", totalsimda);
                                            int rows = update.ExecuteNonQuery();
                                        }
                                } 
with this:

CODE

string updatecorect = "update accounts set totalsimyes=totalsimyes+1 where username = 'DPaul'";
using(SQLiteCommand updatec = new SQLiteCommand(updatecorect, Conexiune))
{
  updatec.ExecuteNonQuery();
} 
And you should do fine. There is absolutely no need to do a select in your case.

Hope this helps.
MakeItSo

ôKnowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.ö (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: SQLite update syntax

(OP)
The error persists..

RE: SQLite update syntax

Sorry, forgit to open the connection:

CODE

string updatecorect = "update accounts set totalsimyes=totalsimyes+1 where username = 'DPaul'";
using(SQLiteCommand updatec = new SQLiteCommand(updatecorect, Conexiune))
{
  Conexiune.Open();
  updatec.ExecuteNonQuery();
  Conexiune.Close();
} 

If this does not work either, please post the entire code that handles this.

ôKnowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.ö (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close