When I tried to insert 5 values into my SQLite Database, it says as follows in the LogCat:
android.database.sqlite.SQLiteException: table student has 3 columns but 5 values were supplied (code 1): , while compiling: INSERT INTO student VALUES('csd13','asdasd','12316456','23','12313');
And this is my table creation code.
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR, name VARCHAR, phone VARCHAR, age VARCHAR, city VARCHAR);");
As you can see, I have initialized 5 fields in the table but why am I not allowed to put in 5 values and it says has ONLY 3 columns.
My insert query is as follow:
db.execSQL("INSERT INTO student VALUES('"+editRollno.getText()+"','"+editName.getText()+ "','"+editPhone.getText()+"','"+editAge.getText()+"','"+editCity.getText()+"');");
It's because I have FIRST initialized the same table name with 3 fields, and then I edited it into 5. So Basically, change the name of the table OR delete the table from the database in Eclipse.
Related
I'm trying to copy the data from one table to other. Please find the below code:
//Dummy table for reference
database.execSQL("CREATE TABLE IF NOT EXISTS `New_Temp1` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `data1` TEXT DEFAULT '', `data2` INTEGER NOT NULL, `data3` TEXT DEFAULT '', `data4` TEXT DEFAULT '', `data5` TEXT DEFAULT '')");
database.execSQL("INSERT INTO New_Temp1 SELECT * FROM Temp1");
database.execSQL("DROP TABLE Temp1");
database.execSQL("ALTER TABLE New_Temp1 RENAME TO Temp1");
The Temp1 table is already there in database. I don't know the schema of that. Means I don't know the columns of that table. So I am creating a new table New_temp1. When I try to copy the data from Temp1 to New_Temp1, application crashes.
**Caused by: android.database.sqlite.SQLiteException: table New_Temp1 has 33 columns but 28 values were supplied (code 1): , while compiling: INSERT INTO New_Temp1 SELECT * FROM Temp1**
How to solve this, I don't know which columns are missing in Temp1 table.
This statement raises the error:
INSERT INTO New_Temp1 SELECT * FROM Temp1
It looks like the target table does not have the same columns as the original (there are more columns in the target).
If you want to copy the table without knowing the structure, you can combine the table creation and the copy in a single statement like that:
CREATE TABLE New_Temp1 AS
SELECT * FROM Temp1
On the other hand, if you don't want to copy all the columns, and you know the corresponding column names in the source table, then you can enumerate them in the select clause:
INSERT INTO New_Temp1 (data1, data2, data3)
SELECT data1, data2, data3 FROM Temp1
You can get the column names of the table Temp1 with this query:
SELECT name FROM pragma_table_info('Temp1')
You can execute the above statement with rawQuery() and by iterating through the rows of the cursor you will have all the column names and you can use them to create the new table.
If you execute:
SELECT * FROM pragma_table_info('Temp1')
you will get additional info for each column, like:
cid name type notnull dflt_value pk
0 column1 INTEGER 0 0
1 column2 INTEGER 0 0
...........................................
I have a SQLite database with one table which I need to update in a new version of my database.
In the first version of the table I have a id and a txt value like this:
db.execSQL("CREATE TABLE table_name id INTEGER PRIMARY KEY AUTOINCREMENT, txt TEXT")
But in the next version of the database I wand to add a lastchanged value which must be a TIMESTAMP with default value current time in epoch which I do with (strftime('%s', 'now')).
But since I can't add a column with default current time, according to this error:
Cannot add a column with non-constant default (code 1): , while compiling: ALTER TABLE table_name ADD COLUMN edited_time TIMESTAMP DEFAULT (strftime('%s', 'now'))
I tought I could best recreate the data and copy it to the new database
// copy all data to single value something like a Cursor res maybe??
db.execSQL("DROP TABLE IF EXISTS table_name );
db.execSQL("CREATE TABLE table_name id INTEGER PRIMARY KEY AUTOINCREMENT, txt TEXT, edited_time TIMESTAMP DEFAULT (strftime('%s', 'now'))");
// now insert the values into the recreated database
But for this i need to copy all data into a new table that does not exist simultanious. Is it posible to do this without looping and having to save every value separate and then inserting them seperately again?
Yes. The general procedure is:
BEGIN;
ALTER TABLE table_name RENAME TO table_name_old;
CREATE TABLE table_name( ... );
INSERT INTO table_name(col1, ..., coln)
SELECT col1, ..., coln FROM table_name_old;
DROP table_name_old;
COMMIT;
I have a column with a NOT NULL constraint. I need to alter the database to remove the constraint. But when I try I get a duplicate column exception from android
db.execSQL("ALTER TABLE " + AnimalContract.DogEntry.TABLE_NAME
+ " ADD COLUMN " + AnimalContract.DogEntry.COLUMN_NAME+" TEXT DEFAULT NULL");
I also tried without DEFAULT
The constraint was UNIQUE NOT NULL
There's no direct way to ALTER COLUMN in SQLite.
I believe your only option is to:
Rename the table to a temporary name
Create a new table without the NOT NULL constraint
Copy the content of the old table to the new one
Remove the old table
This other Stackoverflow answer explains the process in details
Furthermore,
MODIFY COLUMN IN TABLE
You can not use the ALTER TABLE statement to modify a column in SQLite. Instead you will need to rename the table, create a new table, and copy the data into the new table.
Syntax
The syntax to MODIFY A COLUMN in a table in SQLite is:
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE table1 RENAME TO _table1_old;
CREATE TABLE table1 (
( column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
);
INSERT INTO table1 (column1, column2)
SELECT column1, column2
FROM _table1_old;
COMMIT;
PRAGMA foreign_keys=on;
Example
Let's look at an example that shows how to modify a column in a SQLite table.
For example, if we had an employees table that had a column called last_name that was defined as a CHAR datatype:
CREATE TABLE employees
( employee_id INTEGER PRIMARY KEY AUTOINCREMENT,
last_name CHAR NOT NULL,
first_name VARCHAR,
hire_date DATE
);
And we wanted to change the datatype of the last_name field to VARCHAR, we could do the following:
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE employees RENAME TO _employees_old;
CREATE TABLE employees
( employee_id INTEGER PRIMARY KEY AUTOINCREMENT,
last_name VARCHAR NOT NULL,
first_name VARCHAR,
hire_date DATE
);
INSERT INTO employees (employee_id, last_name, first_name, hire_date)
SELECT employee_id, last_name, first_name, hire_date
FROM _employees_old;
COMMIT;
PRAGMA foreign_keys=on;
This example will rename our existing employees table to _employees_old.
Then it will create the new employees table with the last_name field defined as a VARCHAR datatype. Then it will insert all of the data from the _employees_old table into the employees table.
Currently i have a database in android sqllite with many tables of the following column, column A and column B.
Now i am required to have table of column A , column B and column C for subsequent future table created in database. It is alright for me to keep old table of column A and B without changing anything.
so i have the following concern.
1 Let say i have the following code
rssiDB.execSQL("CREATE TABLE IF NOT EXISTS " + rssiTableName + " (ssid VARCHAR, bssid VARCHAR , rssi INTEGER )" );
What is the meaning of if not exists. If i am trying to create a table with a table name that already exists but different number of column, will the creation of table be successful? What will happen actually.
for example i have this table of the table name testing20 with column value of a743954759 , -40
and now i want to create a table of the table name testing20 with column value of peterwifi,a7954759 , -60
will the above code create a table with the same name but different number of column.
2 In a database, is it allowed for database to have many table of different column or is it compulsory for database to have every table to have the exact number of column or column name.
Let say i have a database with one table of table name testing1 with column A and column B. can i now add a table with table name testing2 with column A, column B and column C to the database.
I know i can try this out to find out myself. However i am afraid that it will affect my existing table if i try it out.
Hope someone can answer my question. Thank you
Table are unique objects in database, so you can define two tables with the same name. But you can alter existing tables and add new columns using ALTER TABLE function on your onUpgrade() method, like this :
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// If you need to add a column
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
}
}
Refere to this question How to add new Column to Android SQLite Database? for more info
I copied an SQLite Database example, where I created a table with 3 fields/columns and inserted one record with 3 values, as follows:
>SQLiteDatabase db = openOrCreateDatabase("MyDB",MODE_PRIVATE,null);
>db.execSQL("CREATE TABLE IF NOT EXISTS FunnyNames(Email VARCHAR(255), FirstName VARCHAR(255), LastName VARCHAR(255));");
>db.execSQL("INSERT INTO FunnyNames VALUES('abath#aol.com','Anita','Bath')");
..and I received this error message
>12-28 11:35:00.896: E/SQLiteLog(8857): (1) table FunnyNames has 1 columns but 3 values were supplied
didn't I define the columns correctly?
IF NOT EXISTS only creates the table if it didn't exist. Likely you already have a table with that given name in the database file, and that table only has 1 column.
Uninstall your app or clear its data in the app manager to remove the old database file. Or add DROP TABLE IF EXISTS FunnyNames before the CREATE TABLE.
You've missed the primary key of the table. Every table needs an primary key (which is an unique id) for referencing data. Modify your create statement as follow:
CREATE TABLE IF NOT EXISTS FunnyNames(_id INTEGER PRIMARY KEY AUTOINCREMENT, Email VARCHAR(255), FirstName VARCHAR(255), LastName VARCHAR(255));