SQLiteDatabase AUTO INCREMENT null - android

I have that query
"CREATE TABLE CAdres (id INTEGER AUTO INCREMENT,CariID INTEGER, No INTEGER, Adres TEXT,Tel TEXT,Fax TEXT, NotTanim TEXT,PRIMARY KEY (CariID, No));"
when i inside record , id is null . why null ? normal sql increment number but sqlite come "null".

To get an autoincrementing id column, the column type must be
INTEGER PRIMARY KEY
or
INTEGER PRIMARY KEY AUTOINCREMENT
Your id column INTEGER AUTO INCREMENT is just a regular column which can store NULLs. The AUTO and INCREMENT are not keywords and don't affect anything.
You can also have only one primary key per table so remove the
,PRIMARY KEY (CariID, No)
If you need the combination to be unique, you can change that to a UNIQUE constraint.

try this.. it may work..
"CREATE TABLE CAdres (id INTEGER PRIMARY KEY AUTOINCREMENT,CariID INTEGER NOT NULL, No INTEGER NOT NULL, Adres TEXT,Tel TEXT,Fax TEXT, NotTanim TEXT ,UNIQUE (CariID, No));"
note : diff between primary key and unique is that , unique can be null and primary key cannot be null..

Related

SQLite id auto-increment

I have created a SQLite table by using query below
db.execSQL("create table " + TABLE__WORK + " ( " + ID1 + " INTEGER PRIMARY KEY ,Name TEXT, Title TEXT, Time TEXT, Date TEXT)");
The ID now can be auto-incremented. But after I perform delete query, delete the first row data and add again, the ID will start from 1 instead of 2. Is it possible to make the id become 2 ?
SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table.You do not need ID1.
See reference here
Please use this:
db.execSQL("create table " + TABLE__WORK + " (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,Name TEXT, Title TEXT, Time TEXT, Date TEXT)");
Make it INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL.
Here's what the SQLite Autoincrement document say:
If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY,
that changes the automatic ROWID assignment algorithm to prevent the
reuse of ROWIDs over the lifetime of the database. In other words,
the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs
from previously deleted rows.

SQLite PRIMARY key AutoIncrement doesn't work

I'm trying to a have a table with an auto incremented primary key. The SQL query for table creation is included.
Problem is the auto-increment does not work. Meaning when I insert a row with NULL as the value of conversation_id it just inserts null. I have this problem on multiple tables.
-- Table: conversations
CREATE TABLE conversations (
conversation_id INTEGER (64) PRIMARY KEY
UNIQUE,
target_id BIGINT (64),
sender_id BIGINT (64),
status STRING (16) NOT NULL
DEFAULT unseen,
is_group INTEGER (1) NOT NULL
DEFAULT (0),
last_update INTEGER (32) DEFAULT (0),
target_name STRING (64),
target_photo STRING (256),
unread_count INTEGER (10) DEFAULT (0),
last_message STRING (256)
);
The following is the method I use to insert into table:
public Conversation addConversation(Conversation conversation) {
SQLiteDatabase db = getWritableDatabase();
ContentValues row = new ContentValues();
row.put("target_id", conversation.getTargetID());
row.put("sender_id", conversation.getSenderID());
row.put("target_name", conversation.getTargetName());
row.put("target_photo", conversation.getTargetPhoto());
row.put("status", conversation.getStatus());
row.put("unread_count", conversation.getUnreadCount());
row.put("last_message", conversation.getLastMessage());
conversation.setConversationID(db.insert(TBL_CONVERSATIONS, null, row));
Log.d(TAG, "conversation added: "+conversation.getConversationID());
db.close();
return conversation;
}
The curious thing here is when I retrieve the insert id from insert method it returns the correct value, but the actual database field is null.
If I understand correctly A column declared INTEGER PRIMARY KEY will autoincrement. [Cite]
From documentation:
A table created using CREATE TABLE AS has no PRIMARY KEY and no
constraints of any kind. The default value of each column is NULL.
You don't have to add UNIQUE constraint on a COLUMN that has PRIMARY KEY constraint.
Explanation:
A UNIQUE constraint is similar to a PRIMARY KEY constraint, except
that a single table may have any number of UNIQUE constraints.
Instead add NOT NULL.
This is why:
According to the SQL standard, PRIMARY KEY should always imply NOT
NULL. Unfortunately, due to a bug in some early versions, this is not
the case in SQLite. Unless the column is an INTEGER PRIMARY KEY or the
table is a WITHOUT ROWID table or the column is declared NOT NULL,
SQLite allows NULL values in a PRIMARY KEY column. SQLite could be
fixed to conform to the standard, but doing so might break legacy
applications. Hence, it has been decided to merely document the fact
that SQLite allowing NULLs in most PRIMARY KEY columns.
I recommend using this Column definition:
CREATE TABLE conversations (
conversation_id INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT,
...
}
Most likely the return value you are seeing is the row's ROWID. A ROWID is a hidden column available in every table, unless explicitly removed. According to the official documentation, when you define an INTEGER PRIMARY KEY, it should automatically become an alias for the ROWID. That's also why AUTOINCREMENT is not needed when you define your column in this way.
With one exception noted below, if a rowid table has a primary key
that consists of a single column and the declared type of that column
is "INTEGER" in any mixture of upper and lower case, then the column
becomes an alias for the rowid. Such a column is usually referred to
as an "integer primary key". A PRIMARY KEY column only becomes an
integer primary key if the declared type name is exactly "INTEGER".
Other integer type names like "INT" or "BIGINT" or "SHORT INTEGER" or
"UNSIGNED INTEGER" causes the primary key column to behave as an
ordinary table column with integer affinity and a unique index, not as
an alias for the rowid.
See: CREATE TABLE documentation
Either your column is not an INTEGER, or it is not a PRIMARY KEY. Taking a closer look at your create-statement I can see one or two possible culprits.
UNIQUE vs. PRIMARY KEY
A primary key is unique by default. According to the syntax definition (which you can find on the same documentation page as the citation above) you should choose either PRIMARY KEY or UNIQUE, not both.
COLUMN length restrictions
ROWID is already 64-bit by default. You have specified length 64, but lengths are not specified in bits. You may have specified a 64-byte integer here, which I'm sure was not intended. This should actually not be a problem however, since SQLite ignores length-constraints. So it is not meaningful to specify them.
TLDR
Replace this code:
conversation_id INTEGER (64) PRIMARY KEY UNIQUE
With this:
conversation_id INTEGER PRIMARY KEY AUTOINCREMENT
I just put autoincrement in the query and it works fine .
like this
id integer primary key autoincrement

Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

Mycode
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookdir TEXT , lastaddress TEXT,addresname TEXT PRIMARY KEY(bookdir,lastaddress));";
db.execSQL(createtabBook);
Logcat:
03-05 18:29:31.708: I/System.out(17160): android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error: , while compiling: CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));
i just want to create a table with composite primary key so that it is a combination of bookdir and lastaddress, also i want to make the lid as auto increment.
To get the value of any column autoincrement you need to write it or declare it as primary key.
In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.
EDITED:
As you can not define more than one PRIMARY KEY in a table you have to make the bookdir,lastaddress columns as UNIQUE and define the lid columns as PRIMARY KEY as below:
Try out as below:
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER PRIMARY KEY AUTOINCREMENT,
bookdir TEXT , lastaddress TEXT,addresname TEXT, UNIQUE(bookdir,lastaddress));";
Also add "," after the column addresname TEXT, in your query.

Multi Primary Keys in Android SQLite

In SQLite for Android what is the correct way to add multi primary keys?
Currently I have:
String Create_table = "CREATE TABLE project ( keyId INTEGER PRIMARY KEY, keyName TEXT PRIMARY KEY)";
The alternative method I thought of was:
String Create_table = "CREATE TABLE project (keyID INTEGER, keyName TEXT, PRIMARY KEY(keyID, keyName))";
Are both valid? If so, which is better? Also how do I disallow NULL values?
No it's not possible to create multiple primary keys for a single table. It's basic rule of any SQL. However you can use other constraint like UNIQUE with index to achieve this.
This won't be valid SQL Syntax:
String Create_table = "CREATE TABLE project ( keyId INTEGER PRIMARY KEY, keyName TEXT PRIMARY KEY)";
In other way you can create primary key for multiple column as follows:
Create Table yourTableName (col1, col2, col3, PRIMARY KEY (col1, col2));
How do I disallow NULL values?
you can use NOT NULL Constraint , it will not allow you to enter NULL values.
You may use one primary key , more foreign keys in one table

Sqlite Query Error

I am using sqlite db in android.
In this I need to give Id as autoincrement and second 'Incoming_sms' field as primary key. but it shows me error as below:
detailMessage "near "AUTOINCREMENT": syntax error:
CREATE TABLE TwoWayTable111 (
ID INTEGER AUTOINCREMENT,
INCONMING_MSG TEXT PRIMARY KEY,
OUTGOING_MSG TEXT,
STATUS TEXT )"
Why does this error occur? But when I give id as autoincrement and primary key, it works fine.
This is a FAQ. It works as designed. This statement returns a syntax error.
create table test (id integer autoincrement);
This one runs without error.
create table test (id integer primary key autoincrement);
You should be able to do this.
CREATE TABLE TwoWayTable111 (
ID INTEGER PRIMARY KEY,
INCONMING_MSG TEXT NOT NULL UNIQUE,
OUTGOING_MSG TEXT,
STATUS TEXT );

Categories

Resources