This question already has answers here:
"Integer number too large" error message for 600851475143
(8 answers)
Closed 2 years ago.
I'm trying to store a long number (i.e. 9999999999) on a SQLite INTEGER column, and when I try to use ".put" from ContentValues, it doesn't accept, saying it's larger than an integer number.
The interesting thing is that when I tried to create a long variable, I had the same warning.
Bellow there is the code, witch on AndroiStudio gives me the erros:
PS.: sorry for posting it, because there are so many of these kind of question, but I couldn't find a solution looking them.
public class FeedHelperGamer extends SQLiteOpenHelper {
String TAG = "HelperGamer";
//string to create gamerDB
private static final String SQL_CREATE_ENTRIES_Gamer =
"CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_GAMER + " (" +
_ID + " INTEGER PRIMARY KEY," +
COLUMN_GAMER_NAME+ " TEXT," +
COLUMN_GAMER_GAMES + " INTEGER," +
COLUMN_GAMER_HITS + " INTEGER," +
COLUMN_GAMER_POINTS + " INTEGER," +
COLUMN_GAMER_PICTURE + " BLOB," +
COLUMN_GAMEMODE1 + " TEXT," +
COLUMN_GAMEMODE2 + " TEXT," +
COLUMN_GAMEMODE3 + " TEXT," +
COLUMN_GAMEMODE4 + " TEXT," +
COLUMN_GAMEMODE5 + " TEXT)";
private static final String SQL_DELETE_ENTRIES_GAMER =
"DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_GAMER;
// If you change the database schema, you must increment the database version.
public static final int GAMER_DATABASE_VERSION = 1;
public static final String DATABASE_NAME_GAMER = "Gamer.db";
public FeedHelperGamer(Context context) {
super(context, DATABASE_NAME_GAMER, null, GAMER_DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES_Gamer);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES_GAMER);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
//insere novo jogador
public long addNewGamer(String tmpGamerName, byte[] imageBytes) {
final SQLiteDatabase db = this.getWritableDatabase();
long number = 999999999999999; //it's useless just created to test the long variable
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put( COLUMN_GAMER_NAME, tmpGamerName );
values.put( COLUMN_GAMER_GAMES, 0 );
values.put( COLUMN_GAMER_HITS, 0 );
values.put( COLUMN_GAMER_POINTS, 9999999999);
values.put( COLUMN_GAMER_PICTURE, imageBytes );
values.put( COLUMN_GAMEMODE1, modeStateFalse );
values.put( COLUMN_GAMEMODE2, modeStateFalse );
values.put( COLUMN_GAMEMODE3, modeStateFalse );
values.put( COLUMN_GAMEMODE4, modeStateFalse );
long newRoId = db.insert( FeedReaderContract.FeedEntry.TABLE_GAMER, null, values );
return newRoId;
}
You should change this line
long number = 999999999999999;
like below
long number = 999999999999999L;
Related
I have tried to insert Student Dethasails on my Student Table Data. But this error is shown:
Error inserting STUDENT_ROLL=1 STUDENT_NAME=D _CID=4
android.database.sqlite.SQLiteException: no such table: STUDENT_TABLE (code 1): , while compiling: INSERT INTO STUDENT_TABLE(STUDENT_ROLL,STUDENT_NAME,_CID) VALUES (?,?,?)
This is my code:
Declare Table:-
public static final String STUDENT_TABLE_NAME = "STUDENT_TABLE";
public static final String S_ID = "_SID";
public static final String STUDENT_NAME_KEY = "STUDENT_NAME";
public static final String STUDENT_ROLL_KEY = "STUDENT_ROLL";
public static final String CREATE_STUDENT_TABLE =
"CREATE TABLE " + STUDENT_TABLE_NAME + "(" +
S_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
C_ID + " LONG , " +
STUDENT_NAME_KEY + " VARCHAR(255), " +
STUDENT_ROLL_KEY + " INTEGER , " +
" FOREIGN KEY (" + C_ID + ") REFERENCES " + CLASS_TABLE_NAME + "(" +C_ID+")" +");";
public static final String DROP_STUDENT_TABLE = " DROP TABLE IF EXISTS " + STUDENT_TABLE_NAME;
public static final String SELECT_STUDENT_TABLE = "SELECT * FROM " + STUDENT_TABLE_NAME;
Add Student Method for adding Students. c_id is a foreign key.
long addStudent(long c_id, String studentName, int studentRoll){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(STUDENT_NAME_KEY,studentName);
values.put(STUDENT_ROLL_KEY,studentRoll);
values.put(C_ID, c_id);
try{
long s_id = sqLiteDatabase.insert(STUDENT_TABLE_NAME, null, values);
return s_id;
} catch (SQLException e){
e.printStackTrace();
}
return -1;
}
The problem here as indicated in the logs is that the table doesn't exists. You created the sqlite statement for creation of the table but I think you didn't execute it from anywhere. You must execute the sqlite statement in your Database helper class:
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_STUDENT_TABLE);
}
More information can be found here
you created the query to create your table but it has not been executed anywhere, you just have to execute it before performing an insert
database.execSQL(CREATE_STUDENT_TABLE);
I am creating two tables, one for books and one for stores.I want to link every Book in BookStore Table to StoreTable by giving it a Store_ID.
That Store_Id will be the primary key in StoreTable.I am having trouble with the syntax of the foreign key.I also checked http://www.sqlite.org/foreignkeys.html for reference but that didn't provide me with enough clarification.I would be much obliged if anyone helps me.
public class DatabaseHelper extends SQLiteOpenHelper{
//Name of databases
public static final String DATABASE_NAME = "Library.db";
//Version of database
public static final int DATABASE_VERSION = 1;
//Table of Stores
public static final String STORE_TABLE = "Store_Table";
public static final String STORE_ID = "Store_ID";
public static final String STORE_NAME = "Store_name";
public static final String STORE_Address = "Store_Address";
public static final String STORE_LAT = "lat";
public static final String STORE_LNG = "lng";
//Table of Books
public static final String BOOKS_TABLE = "Books_Table";
public static final String BOOK_ID = "Book_ID";
public static final String BOOK_NAME = "Book_name";
public static final String BOOK_AUTHOR = "Book_Author";
public static final String BOOKStore = "BookStore_ID";
//Creating Stores Table
private static final String SQL_CREATE_TABLE_STORE = "CREATE TABLE " + STORE_TABLE + "("
+ STORE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ STORE_NAME + " TEXT NOT NULL, "
+ STORE_Address + " TEXT NOT NULL, "
+ STORE_LAT + " TEXT NOT NULL, "
+ STORE_LNG + " TEXT NOT NULL"
+");";
//Creating Books Table
private static final String SQL_CREATE_TABLE_BOOKS = "CREATE TABLE " + BOOKS_TABLE + "("
+ BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BOOK_NAME + " TEXT NOT NULL, "
+ BOOK_AUTHOR + " TEXT NOT NULL, "
//How to relate BookStore with Store_ID here?
+FOREIGN KEY(BOOKStore) REFERENCES STORE_TABLE(STORE_ID));
+");";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase db = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQL_CREATE_TABLE_STORE);
sqLiteDatabase.execSQL(SQL_CREATE_TABLE_BOOKS);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
//Clear all data
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + STORE_TABLE);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + BOOKS_TABLE);
//RECREAT THE TABLES
onCreate(sqLiteDatabase);
}
}
I see multiple problems with the CREATE TABLE statement for the Books_Table. First, if you want to designate a column in a table as a foreign key, that column first has to exist. You were referring to the BookStore_ID column when defining your foreign key, but you never actually defined this column. Second, if you want to mark a column as a foreign key, there is a specific syntax. See below for details.
CREATE TABLE Books_Table (
Book_ID INTEGER PRIMARY KEY AUTOINCREMENT,
Book_name TEXT NOT NULL,
Book_Author TEXT NOT NULL,
BookStore_ID INTEGER,
CONSTRAINT fk_bookstore FOREIGN KEY (BookStore_ID)
REFERENCES Store_Table(Store_ID)
);
This is the correct answer:
private static final String SQL_CREATE_TABLE_BOOKS = "CREATE TABLE " + BOOKS_TABLE + "("
+ BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BOOK_NAME + " TEXT NOT NULL, "
+ BOOK_AUTHOR + " TEXT NOT NULL, "
//How to relate BookStore with Store_ID here?
+ " FOREIGN KEY ("+ BOOKStore +") REFERENCES "+STORE_TABLE+"("+STORE_ID+"));";
I have another problem with SQLite database in Android OS.
I'm trying to create database with two tables: departments and students.
Departments table contains: dep_id, dep_name and dep_major.
Students table contains: stud_id, dep_id, firstname, surname, email, phone.
How can I put data into this two tables?
There is a Databasehandler.java:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String TAG = "DatabaseHandler";
private static final String DATABASE_NAME = "studentsManager2.db";
// pola tabeli "wydzial"
private static final String TABLE_DEPARTMENTS = "departments",
KEY_DEP_ID = "id",
KEY_DEPARTMENT = "wydzial",
KEY_SPECIALIZATION = "kierunek";
// pola tabeli "students"
private static final String TABLE_STUDENTS = "students",
KEY_STUD_ID = "id",
KEY_ID = KEY_DEP_ID,
KEY_FIRSTNAME = "imie",
KEY_SURNAME = "nazwisko",
KEY_INDEKS = "indeks",
KEY_EMAIL = "email",
KEY_NUMER = "numer";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_DEPARTMENTS + "(" + KEY_DEP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_DEPARTMENT + " TEXT NOT NULL," + KEY_SPECIALIZATION + " TEXT NOT NULL)");
db.execSQL("CREATE TABLE " + TABLE_STUDENTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FIRSTNAME + " TEXT NOT NULL," + KEY_SURNAME + " TEXT NOT NULL," + KEY_INDEKS + " INTEGER," + KEY_EMAIL + " TEXT NOT NULL," + KEY_NUMER + " TEXT NOT NULL)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEPARTMENTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENTS);
onCreate(db);
}
}
Below methods will insert the records in the sqlite database using Datbase helper class:
public void insertDataInBothTables() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues valuesForDepartmentTable = new ContentValues();
valuesForDepartmentTable.put(KEY_DEPARTMENT , "name");
valuesForDepartmentTable.put(KEY_SPECIALIZATION , "special");
// insert row
long idOfDepart = db.insert(TABLE_DEPARTMENTS, null, valuesForDepartmentTable);
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, "first-name");
values.put(KEY_SURNAME, "sur-name");
values.put(KEY_INDEKS , "inserting-data");
values.put(KEY_EMAIL , "inserting-data");
values.put(KEY_NUMER , "inserting-data");
//Parameter to add department id
values.put(KEY_WYDZIAL, idOfDepart + "");
// insert row
db.insert(TABLE_STUDENTS, null, values);
}
I have to create 2 to 3 tables and I want to create foreign key of two tables and add into 3rd table. When I run the app data is inserted successfully but foreign key is not showing in the database table structure and table.
Here is my Database class:
public static final String Tsk_id = "task_id";
public static final String Task_Project_NAme = "project_name";
public static final String Task_Team_Member_Name = "assign_to";
public static final String Task_Title = "task_time";
public static final String Task_Start_Date = "start_date";
public static final String Task_CompletionDate = "completion_date";
public static final String Task_CompletionTime = "completion_time";
public static final String Task_Description = "task_description";
public static final String Task_Status = "task_status";
public static final String Task_Is_Active = "IsActive";
public static final String Task_Project_id="project_id";
public static final String Task_Team_Memmber_Id="team_member_id";
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TASK_TABLE = "CREATE TABLE " + ASSIGN_TASK_TABLE + "("
+ Tsk_id +" integer primary key autoincrement, "
+ Task_Project_NAme + " TEXT,"
+ Task_Team_Member_Name + " TEXT,"
+ Task_Title + " TEXT,"
+ Task_Start_Date + " TEXT,"
+ Task_CompletionDate + " TEXT,"
+ Task_CompletionTime + " TEXT,"
+ Task_Description + " TEXT,"
+ Task_Status + " TEXT,"
+ Task_Is_Active + " TEXT"
+ " FOREIGN KEY ("+Task_Project_id+") REFERENCES " +CONTACTS_TABLE_NAME+" ("+Project_id+")"
+ " FOREIGN KEY ("+Task_Team_Memmber_Id+") REFERENCES " +DEFINE_TEAM_MEMBER_TABLE+" ("+Team_Member_id+")";
db.execSQL(CREATE_TASK_TABLE);
}
public boolean insertTaskDetails(String strTask_Project_NAme,String strTask_Team_Member_Name,
String strTask_Title,String strTask_Start_Date,String strTask_CompletionDat,
String strTask_CompletionTime,String strTask_Description,
String strTask_Status,String strTask_Is_Active)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Task_Project_NAme, strTask_Project_NAme);
contentValues.put(Task_Team_Member_Name, strTask_Team_Member_Name);
contentValues.put(Task_Title, strTask_Title);
contentValues.put(Task_Start_Date, strTask_Start_Date);
contentValues.put(Task_CompletionDate,strTask_CompletionDat);
contentValues.put(Task_CompletionTime,strTask_CompletionTime);
contentValues.put(Task_Description, strTask_Description);
contentValues.put(Task_Status, strTask_Status);
contentValues.put(Task_Is_Active,strTask_Is_Active);
db.insert(ASSIGN_TASK_TABLE, null, contentValues);
db.close();
return true;
}
+ " FOREIGN KEY ("+Task_Project_id+") REFERENCES " +CONTACTS_TABLE_NAME+" ("+Project_id+")"
for this line of code where is Task_Project_id column in the while creating the table.. please check.
You need to create a particular column to reference as foreign key.
same with this line too
" FOREIGN KEY ("+Task_Team_Memmber_Id+") REFERENCES " +DEFINE_TEAM_MEMBER_TABLE+" ("+Team_Member_id+")";
I am relatively new to using sqlite in the android world and I currently I my sqlite database set up like the following:
db.execSQL("CREATE TABLES " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LOCKERNUMBER
+ " TEXT NOT NULL, " + KEY_STATUS + " INTEGER DEFAULT 0, "
+ KEY_PIN + " INTEGER DEFAULT 0);");
And I have the following methods to insert data into the database:
// function to add lockers to database
public long createLockerEntry(String lockerNumber, int status) {
ContentValues cv = new ContentValues();
cv.put(KEY_LOCKERNUMBER, lockerNumber);
cv.put(KEY_STATUS, status);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
// function to create a pin for the locker
public void createPin(String lockerNumber, int pin, int status) {
}
// function to remove pin
public void removePin(String lockerNumber, int pin, int status) {
}
My intentions are to add a pin number for a certain locker number to the KEY_PIN column within the method Create pin and then change the status value to 1. I would assume I would use the where clause statement but I am not completely sure about the syntax or if that is the write approach. Any suggestions?
The method you want is update()
You can read about it by searching for "update" on this page:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
One way to use this would be:
// function to create a pin for the locker
public void createPin(tring lockerNumber, int pin, int status) {
ContentValues cv = new ContentValues()
cv.put(KEY_PIN, pin);
cv.put(KEY_STATUS, status);
ourDatabase.update(DATABASE_TABLE, cv, KEY_LOCKERNUMBER + " = ?"
, new String[] {lockerNumber});
}
Here's an example where I toggle the value of an entry. It is an int value used as binary (i.e. 1 or 0).
The two methods checkAsWildTest, and uncheckAsWildTest.
public void checkAsWildTest(Cursor c, int val) {
assertDbOpen();
String[] _params = { String.valueOf(c.getInt(ID_COLUMN)) };
//next 4 lines are just for debug
log(".uncheckAsWildTest(), " + "c.getCount() = " + c.getCount());
log("row selected = " + c.getInt(ID_COLUMN));
log("cursor content: " + c.getString(ANSWERS_RESPONSE_COLUMN));
log("cursor columns" + Arrays.toString(c.getColumnNames()));
ContentValues cv = new ContentValues();
cv.put(KEY_ANSWERS_CHECKED, val);
db.update(ANSWERS_TABLE, cv, "_id=?", _params);
}
public void uncheckAsWildTest(Cursor c) {
assertDbOpen();
int unchecked = 0;
String[] _params = { String.valueOf(c.getInt(ID_COLUMN)) };
//next 4 lines are just for debug
log(".uncheckAsWildTest(), " + "c.getCount() = " + c.getCount());
log("column selected = " + c.getInt(ID_COLUMN));
log("cursor content: " + c.getString(ANSWERS_RESPONSE_COLUMN));
log("cursor columns" + Arrays.toString(c.getColumnNames()));
ContentValues cv = new ContentValues();
cv.put(KEY_ANSWERS_CHECKED, unchecked);
db.update(ANSWERS_TABLE, cv, "_id=?", _params);
}
I believe you can work with this, and modify it for your purposes.