Mathlight
Mathlight

Reputation: 6653

SQLite doesn't return what it should return

Everything works fine, except one thing. When i try to pull the username of the one with the highest score, the program fails.

The result that i get is 0 instead of le_user

This is the part of code that act's weird:

string query = "SELECT * FROM \"Highscore_List\" ORDER BY Highscore_List.score DESC LIMIT 1";
DataTable  recipe = db.GetDataTable(query);
foreach (DataRow r in recipe.Rows)
{
    string old_user = r["user_name"].ToString();
    int oude_topscore = Convert.ToInt32(r["score"]);
}

And the score is right, just the user name not. And when i leave this part of the query

ORDER BY Highscore_List.score DESC LIMIT 1

The result is still the same. So that isn't the problem.

So what am I doing wrong?

EDIT:

Here is an SQL dump of it:

-- ----------------------------
-- Table structure for "main"."Highscore_List"
-- ----------------------------
DROP TABLE "main"."Highscore_List";
CREATE TABLE "Highscore_List" (
"id"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"score"  INTEGER NOT NULL,
"user_name"  INTEGER NOT NULL
);

-- ----------------------------
-- Records of Highscore_List
-- ----------------------------
INSERT INTO "main"."Highscore_List" VALUES (1, 1, 'hui');
INSERT INTO "main"."Highscore_List" VALUES (2, 2, 'Onbekend');
INSERT INTO "main"."Highscore_List" VALUES (3, 3, 'onbekend_tt');
INSERT INTO "main"."Highscore_List" VALUES (4, 2, 'onbekenddd');
INSERT INTO "main"."Highscore_List" VALUES (5, 6, 'le_user');
INSERT INTO "main"."Highscore_List" VALUES (6, 0, 'Onbekend');

Upvotes: 0

Views: 128

Answers (1)

Stephan
Stephan

Reputation: 4247

You have created your column "user_name" as an integer column.

Actually, it is ok for SQLite to put text values into an integer column and read it out again as text. However, I'm not familiar with the ADO.NET interface to SQLite. It seems that it reads out "user_name" values as integer which results in the 0 values. I see two ways to solve the problem:

  1. Create the "user_name" column as TEXT
  2. Find a way to tell the ADO.NET-SQLite-bridge that it should interpret the values in the "user_name" column of query results as TEXT

Upvotes: 1

Related Questions