can't insert values to a SQLite table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm having hard time figuring out why im getting an error when I inserting values to the table.
thanks for the help.
Basically,the code worked when it only with two column and one contentValues.put function but,when I added another column i keep getting error trying to inset values and i don't know why.
I have checked the SQlite query etc..
package com.avrahamzilberblat.battleshipfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "scoreRatio";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, 5);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name,Double scoreRatio){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'" + " AND " +COL3 + " = '" + scoreRatio+"'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
public void clearDatabase(String TABLE_NAME) {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
}
--------------------This is the update i want a string and a double data type,still doesn't work
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3 +"REAL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, "ood");
contentValues.put(COL3, 6.6);
android android-sqlite
|
show 1 more comment
I'm having hard time figuring out why im getting an error when I inserting values to the table.
thanks for the help.
Basically,the code worked when it only with two column and one contentValues.put function but,when I added another column i keep getting error trying to inset values and i don't know why.
I have checked the SQlite query etc..
package com.avrahamzilberblat.battleshipfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "scoreRatio";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, 5);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name,Double scoreRatio){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'" + " AND " +COL3 + " = '" + scoreRatio+"'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
public void clearDatabase(String TABLE_NAME) {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
}
--------------------This is the update i want a string and a double data type,still doesn't work
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3 +"REAL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, "ood");
contentValues.put(COL3, 6.6);
android android-sqlite
In yourCREATE TABLE
query, you mentioned all column's datatype as aTEXT
and in youaddData()
method, you are trying to add integer value inCOL3
. Might be the issue.
– Ajay - Rlogical
Jan 4 at 9:15
There should be a space betweenCOL3
and"TEXT"
in theCREATE TABLE
command, and a semicolon after the closing bracket.
– Susmit Agrawal
Jan 4 at 9:15
I have played with it a lot i actually want it to be a real number such as Double so in the beginning i wrote REAL instead of TEXT
– Omer Michleviz
Jan 4 at 9:19
i added some changes @SusmitAgrawal and it still doesn't work
– Omer Michleviz
Jan 4 at 9:25
You altered/recreated the table?
– Susmit Agrawal
Jan 4 at 10:10
|
show 1 more comment
I'm having hard time figuring out why im getting an error when I inserting values to the table.
thanks for the help.
Basically,the code worked when it only with two column and one contentValues.put function but,when I added another column i keep getting error trying to inset values and i don't know why.
I have checked the SQlite query etc..
package com.avrahamzilberblat.battleshipfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "scoreRatio";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, 5);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name,Double scoreRatio){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'" + " AND " +COL3 + " = '" + scoreRatio+"'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
public void clearDatabase(String TABLE_NAME) {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
}
--------------------This is the update i want a string and a double data type,still doesn't work
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3 +"REAL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, "ood");
contentValues.put(COL3, 6.6);
android android-sqlite
I'm having hard time figuring out why im getting an error when I inserting values to the table.
thanks for the help.
Basically,the code worked when it only with two column and one contentValues.put function but,when I added another column i keep getting error trying to inset values and i don't know why.
I have checked the SQlite query etc..
package com.avrahamzilberblat.battleshipfinal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "scoreRatio";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, 5);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* @param name
* @return
*/
public Cursor getItemID(String name,Double scoreRatio){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'" + " AND " +COL3 + " = '" + scoreRatio+"'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
public void clearDatabase(String TABLE_NAME) {
SQLiteDatabase db = this.getWritableDatabase();
String clearDBQuery = "DELETE FROM "+TABLE_NAME;
db.execSQL(clearDBQuery);
}
}
--------------------This is the update i want a string and a double data type,still doesn't work
@Override
public void onCreate(SQLiteDatabase db) {
//String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +COL2 +" TEXT,"+ COL3 +"INTEGER"+");";
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3 +"REAL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item,int scoreRatio) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, "ood");
contentValues.put(COL3, 6.6);
android android-sqlite
android android-sqlite
edited Jan 4 at 10:29
Fantômas
32.9k156491
32.9k156491
asked Jan 4 at 9:12
Omer MichlevizOmer Michleviz
287
287
In yourCREATE TABLE
query, you mentioned all column's datatype as aTEXT
and in youaddData()
method, you are trying to add integer value inCOL3
. Might be the issue.
– Ajay - Rlogical
Jan 4 at 9:15
There should be a space betweenCOL3
and"TEXT"
in theCREATE TABLE
command, and a semicolon after the closing bracket.
– Susmit Agrawal
Jan 4 at 9:15
I have played with it a lot i actually want it to be a real number such as Double so in the beginning i wrote REAL instead of TEXT
– Omer Michleviz
Jan 4 at 9:19
i added some changes @SusmitAgrawal and it still doesn't work
– Omer Michleviz
Jan 4 at 9:25
You altered/recreated the table?
– Susmit Agrawal
Jan 4 at 10:10
|
show 1 more comment
In yourCREATE TABLE
query, you mentioned all column's datatype as aTEXT
and in youaddData()
method, you are trying to add integer value inCOL3
. Might be the issue.
– Ajay - Rlogical
Jan 4 at 9:15
There should be a space betweenCOL3
and"TEXT"
in theCREATE TABLE
command, and a semicolon after the closing bracket.
– Susmit Agrawal
Jan 4 at 9:15
I have played with it a lot i actually want it to be a real number such as Double so in the beginning i wrote REAL instead of TEXT
– Omer Michleviz
Jan 4 at 9:19
i added some changes @SusmitAgrawal and it still doesn't work
– Omer Michleviz
Jan 4 at 9:25
You altered/recreated the table?
– Susmit Agrawal
Jan 4 at 10:10
In your
CREATE TABLE
query, you mentioned all column's datatype as a TEXT
and in you addData()
method, you are trying to add integer value in COL3
. Might be the issue.– Ajay - Rlogical
Jan 4 at 9:15
In your
CREATE TABLE
query, you mentioned all column's datatype as a TEXT
and in you addData()
method, you are trying to add integer value in COL3
. Might be the issue.– Ajay - Rlogical
Jan 4 at 9:15
There should be a space between
COL3
and "TEXT"
in the CREATE TABLE
command, and a semicolon after the closing bracket.– Susmit Agrawal
Jan 4 at 9:15
There should be a space between
COL3
and "TEXT"
in the CREATE TABLE
command, and a semicolon after the closing bracket.– Susmit Agrawal
Jan 4 at 9:15
I have played with it a lot i actually want it to be a real number such as Double so in the beginning i wrote REAL instead of TEXT
– Omer Michleviz
Jan 4 at 9:19
I have played with it a lot i actually want it to be a real number such as Double so in the beginning i wrote REAL instead of TEXT
– Omer Michleviz
Jan 4 at 9:19
i added some changes @SusmitAgrawal and it still doesn't work
– Omer Michleviz
Jan 4 at 9:25
i added some changes @SusmitAgrawal and it still doesn't work
– Omer Michleviz
Jan 4 at 9:25
You altered/recreated the table?
– Susmit Agrawal
Jan 4 at 10:10
You altered/recreated the table?
– Susmit Agrawal
Jan 4 at 10:10
|
show 1 more comment
1 Answer
1
active
oldest
votes
You should change :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
to :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" TEXT)");
Then you should do one of the following :-
- Delete(Clear) the App's data from settings/apps.
- uninstall the App.
Increase the database version e.g. change
super(context, TABLE_NAME, null, 1);
tosuper(context, TABLE_NAME, null, 2);
and then rerun the App.
The above is important as onCreate only ever runs automatically once, when the database is created, which is perhaps the root cause of you issue. You've added a column but it actually hasn't been added.
I have played with it a lot i actually want it to be a real number
such as Double so in the beginning i wrote REAL instead of TEXT
SQlite allows any type of data to be stored in any type of column with one exception and that is an alias of the rowid (column defined with INTEGER PRIMARY KEY (with or without AUTOINCREMENT)). An alias of rowid can only store a integer (signed 64 bit) and it must be UNIQUE within the table.
- Note AUTOINCREMENT has been omitted. You very likely don't need it and it's got overheads
SQLite Autoincrement
Which includes :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed.
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54035924%2fcant-insert-values-to-a-sqlite-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should change :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
to :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" TEXT)");
Then you should do one of the following :-
- Delete(Clear) the App's data from settings/apps.
- uninstall the App.
Increase the database version e.g. change
super(context, TABLE_NAME, null, 1);
tosuper(context, TABLE_NAME, null, 2);
and then rerun the App.
The above is important as onCreate only ever runs automatically once, when the database is created, which is perhaps the root cause of you issue. You've added a column but it actually hasn't been added.
I have played with it a lot i actually want it to be a real number
such as Double so in the beginning i wrote REAL instead of TEXT
SQlite allows any type of data to be stored in any type of column with one exception and that is an alias of the rowid (column defined with INTEGER PRIMARY KEY (with or without AUTOINCREMENT)). An alias of rowid can only store a integer (signed 64 bit) and it must be UNIQUE within the table.
- Note AUTOINCREMENT has been omitted. You very likely don't need it and it's got overheads
SQLite Autoincrement
Which includes :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed.
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
add a comment |
You should change :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
to :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" TEXT)");
Then you should do one of the following :-
- Delete(Clear) the App's data from settings/apps.
- uninstall the App.
Increase the database version e.g. change
super(context, TABLE_NAME, null, 1);
tosuper(context, TABLE_NAME, null, 2);
and then rerun the App.
The above is important as onCreate only ever runs automatically once, when the database is created, which is perhaps the root cause of you issue. You've added a column but it actually hasn't been added.
I have played with it a lot i actually want it to be a real number
such as Double so in the beginning i wrote REAL instead of TEXT
SQlite allows any type of data to be stored in any type of column with one exception and that is an alias of the rowid (column defined with INTEGER PRIMARY KEY (with or without AUTOINCREMENT)). An alias of rowid can only store a integer (signed 64 bit) and it must be UNIQUE within the table.
- Note AUTOINCREMENT has been omitted. You very likely don't need it and it's got overheads
SQLite Autoincrement
Which includes :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed.
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
add a comment |
You should change :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
to :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" TEXT)");
Then you should do one of the following :-
- Delete(Clear) the App's data from settings/apps.
- uninstall the App.
Increase the database version e.g. change
super(context, TABLE_NAME, null, 1);
tosuper(context, TABLE_NAME, null, 2);
and then rerun the App.
The above is important as onCreate only ever runs automatically once, when the database is created, which is perhaps the root cause of you issue. You've added a column but it actually hasn't been added.
I have played with it a lot i actually want it to be a real number
such as Double so in the beginning i wrote REAL instead of TEXT
SQlite allows any type of data to be stored in any type of column with one exception and that is an alias of the rowid (column defined with INTEGER PRIMARY KEY (with or without AUTOINCREMENT)). An alias of rowid can only store a integer (signed 64 bit) and it must be UNIQUE within the table.
- Note AUTOINCREMENT has been omitted. You very likely don't need it and it's got overheads
SQLite Autoincrement
Which includes :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed.
You should change :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT,"+COL3+"TEXT)");
to :-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" TEXT)");
Then you should do one of the following :-
- Delete(Clear) the App's data from settings/apps.
- uninstall the App.
Increase the database version e.g. change
super(context, TABLE_NAME, null, 1);
tosuper(context, TABLE_NAME, null, 2);
and then rerun the App.
The above is important as onCreate only ever runs automatically once, when the database is created, which is perhaps the root cause of you issue. You've added a column but it actually hasn't been added.
I have played with it a lot i actually want it to be a real number
such as Double so in the beginning i wrote REAL instead of TEXT
SQlite allows any type of data to be stored in any type of column with one exception and that is an alias of the rowid (column defined with INTEGER PRIMARY KEY (with or without AUTOINCREMENT)). An alias of rowid can only store a integer (signed 64 bit) and it must be UNIQUE within the table.
- Note AUTOINCREMENT has been omitted. You very likely don't need it and it's got overheads
SQLite Autoincrement
Which includes :-
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed.
edited Jan 4 at 10:29
answered Jan 4 at 10:17
MikeTMikeT
18.3k112844
18.3k112844
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
add a comment |
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Finally , A solid answer.Thanks!.
– Omer Michleviz
Jan 4 at 10:30
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
Adding the missing space and executing step 2 is enough... ;)
– Fantômas
Jan 4 at 10:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54035924%2fcant-insert-values-to-a-sqlite-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In your
CREATE TABLE
query, you mentioned all column's datatype as aTEXT
and in youaddData()
method, you are trying to add integer value inCOL3
. Might be the issue.– Ajay - Rlogical
Jan 4 at 9:15
There should be a space between
COL3
and"TEXT"
in theCREATE TABLE
command, and a semicolon after the closing bracket.– Susmit Agrawal
Jan 4 at 9:15
I have played with it a lot i actually want it to be a real number such as Double so in the beginning i wrote REAL instead of TEXT
– Omer Michleviz
Jan 4 at 9:19
i added some changes @SusmitAgrawal and it still doesn't work
– Omer Michleviz
Jan 4 at 9:25
You altered/recreated the table?
– Susmit Agrawal
Jan 4 at 10:10