I have two tables in my database. Those are user and message.
User table contains with,
Userid (Primary key)
UserName
Password columns
Message table contains with,
MessageId (Primary Key)
Userid (Foreign key)
Subject
Message columns
After login user can send a message. So I want to save the above column details in the message table. But when I fill the table, userid is not getting as a foreign key. That column is empty like the following image.
These are my dbHandler code
public void onCreate(SQLiteDatabase db) {
String TABLE_USER_CREATE = "CREATE TABLE "+ UserTable.Users.TABLE_NAME+"("+
UserTable.Users._ID+" INTEGER PRIMARY KEY,"+
UserTable.Users.COLUMN_NAME+" TEXT,"+
UserTable.Users.COLUMN_PASSWORD+ " TEXT,"+
UserTable.Users.COLUMN_TYPE+ " TEXT"+" );";
db.execSQL(TABLE_USER_CREATE);
String TABLE_MESSAGE_CREATE = "CREATE TABLE "+ MessageTable.Messages.TABLE_NAME+"("+
MessageTable.Messages._ID+" INTEGER PRIMARY KEY,"+
MessageTable.Messages.COLUMN_USER+" TEXT,"+
MessageTable.Messages.COLUMN_SUBJECT+ " TEXT,"+
MessageTable.Messages.COLUMN_MESSAGE+" TEXT,"+
" FOREIGN KEY ("+ MessageTable.Messages.COLUMN_USER+") REFERENCES "+ UserTable.Users.TABLE_NAME+"("+ UserTable.Users._ID+"))";
db.execSQL(TABLE_MESSAGE_CREATE);
}
Save message Method:
public long saveMessage(String subjects, String messages){
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MessageTable.Messages.COLUMN_SUBJECT,subjects);
contentValues.put(MessageTable.Messages.COLUMN_MESSAGE,messages);
long newRowId = db.insert(MessageTable.Messages.TABLE_NAME,null,contentValues);
db.close();
return newRowId; }
I'm new to android. Can somebody help me to fix this?
It's NULL because you never set it. Foreign keys aren't magic, they're just a constraint that whatever value you add must be present in the referenced column.
As you also didn't add a NOT NULL constraint, it just adds exactly what you told it to - only the subject and message.
Related
Go easy on me, middle school teacher taking a CS class. I've got a Java program that asks for user name, height, weight, does some calculations and gives results to the user. I now need to store this data in a database. I can get the data to store until I start using primary and foreign keys.
Here is the error I can't figure out:
Error: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL180429151131780' defined on 'USERPROFILE'.
Here is my table:
drop table stayfitapp.userdata;
drop table stayfitapp.userprofile;
drop schema stayfitapp restrict;
create schema stayfitapp;
create table stayfitapp.userprofile
(
profileName varchar(255) not null primary key,
profileGender varchar(255) not null
);
create table stayfitapp.userdata
(
profileAge double not null,
profileWeight double not null,
profileHeight double not null,
profileWaistCircumference double not null,
profileHipCircumference double not null,
profileName varchar(255),
foreign key (profileName) references stayfitapp.userprofile(profileName)
);
Here is the section of the "app" that writes to the table...
public void save(){
try {
String query = "insert into stayfitapp.userprofile" + "(profileName, profileGender)" + "values" + "(?,?)";
String query2 = "insert into stayfitapp.userdata" + "(profileAge, profileWeight, profileHeight, profileWaistCircumference, profileHipCircumference)" + "values" + "(?,?,?,?,?)";
Connection myConnection = DriverManager.getConnection("jdbc:derby://localhost:1527/stayfitDB2", "username", "password");
Statement myStatement = myConnection.createStatement();
//Statement myStatement2 = myConnection.createStatement();
PreparedStatement prepared = myConnection.prepareStatement(query);
prepared.setString(1, profileName);
prepared.setString(2, profileGender);
PreparedStatement prepared2 = myConnection.prepareStatement(query2);
prepared2.setDouble(1, profileAge);
prepared2.setDouble(2, profileWeight);
prepared2.setDouble(3, profileHeight);
prepared2.setDouble(4, profileWaistCircumference);
prepared2.setDouble(5, profileHipCircumference);
int rowsAffected = prepared.executeUpdate();
int rowsAffected2 = prepared2.executeUpdate();
if(rowsAffected==0)
{
System.out.println("Warning: User data did not save!");
}
else
{
System.out.println("User info saved!");
}
}
catch(SQLException e)
{
System.out.println("Error: "+e.toString());
}
Your save() method will attempt to add the user to the stayfitapp.userprofile table. This table has a field called profileName. profileName is the "primary key" so no duplicate values are allowed.
The error that you are getting is saying that you cannot add(insert) the record to the table because the table already has a record with the same name.
Does your program work okay if you use a different name each time?
You will need to add some logic to your program to deal with the scenario where the profileName already exists in the table. This will probably involve deleting or updating the existing record.
This is the problem.
insert into stayfitapp.userprofile"
+ "(profileName, profileGender)" + "values" , etc
You have nothing to check to see if a record already exists. Something like this would work better.
insert into stayfitapp.userprofile
profileName, profileGender
select distinct ?, ?
from someSmallTable
where not exists (
select 1
from stayfitapp.userprofile
where profileName = ?
)
The someSmallTable bit depends on your database engine, which you didn't specify.
I ended up writing a method to check if the username was already in the profile table. If the username was a duplicate I only wrote to the data table. If the username was new I wrote to both tables.
Thank you for your help! I'm sure there was a more efficient method (figuratively and literally) but I'm on to my final project and nearly surviving an actual CS class.
My create table statement:
CREATE TABLE main_table (
_ID INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL,
employee_id INTEGER NOT NULL,
rating REAL NOT NULL,
timestamp LONG NOT NULL,
FOREIGN KEY(account_id) REFERENCES ACCOUNTS(_ID),
FOREIGN KEY(employee_id) REFERENCES EMPLOYEES(_ID),
UNIQUE (account_id, employee_id, timestamp));
However now if I do something like
public void deleteAccount(long accountId) {
mDatabase.delete(
ACCOUNTS,
_ID + " = ?",
new String[]{accountId + ""}
);
}
Which is basically DELETE FROM ACCOUNTS WHERE _ID = some_id but it isn't cascading through the other tables (employees or main_table) and deleting any records there that link up through the foreign keys.
I do have this code as well:
#Override
public void onConfigure(SQLiteDatabase db){
db.setForeignKeyConstraintsEnabled(true);
}
Am I misunderstanding how these work? Isn't this what foreign keys are for? Why isn't it cascading?
You have to mention explicitly if you want any cascading
Change the query like below
CREATE TABLE main_table (
_ID INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL,
employee_id INTEGER NOT NULL,
rating REAL NOT NULL,
timestamp LONG NOT NULL,
FOREIGN KEY(account_id) REFERENCES ACCOUNTS(_ID) ON DELETE CASCADE,
FOREIGN KEY(employee_id) REFERENCES EMPLOYEES(_ID) ON DELETE CASCADE,
UNIQUE (account_id, employee_id, timestamp));
I have a database created in android studio to save data to . The table is not being created in the database . The error I'm getting is :
03-04 00:55:45.783 25787-25787/com/SQLiteLog: (1) no such table: TableName
03-04 00:55:45.783 25787-25787/com.E/SQLiteDatabase: Error inserting
My code for creating the table is :
#Override
public void onCreate(SQLiteDatabase db) {
db = SQLiteDatabase.openOrCreateDatabase("DB.ad",null);
Log.v(TAG, "*TABLE ");
// create TableName table
db.execSQL(CREATE_TableName_TABLE);
this.checkDataBase();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (SQLiteException e) {
// database doesn't exist yet.
}
return checkDB != null;
}
I was wondering if anyone can tell me why I am getting this error?
String CREATE_TableName_TABLE = "CREATE TABLE TableNames( " +
"Id INT( 30 ) PRIMARY KEY AUTOINCREMENT, " +
"TagNo TEXT, " +
"Description TEXT, " +
"WeeksGone INTEGER( 10 ) NOT NULL DEFAULT 0 );";
Here is an example how to create your database. First your class should extends from SQLiteOpenHelper
Then use super to SQLiteOpenHelper class with your db name and version,
public YourClassName(Context context) {
super(context, "YourDatabaseName", null, 1); // 1 is the version of the database, after each change in your db you should ++.
}
Then create your table:
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
final String SQL_CREATE_BLABLA_TABLE = "CREATE TABLE " + "TableNameHere" + " (" +
"_id" + " INTEGER PRIMARY KEY," +
"name" + " TEXT NOT NULL);";
sqLiteDatabase.execSQL(SQL_CREATE_BLABLA_TABLE);
}
And finally in case you need to upgrade your db version, because you make any change override the onUpgrade method, in order to create again the table.
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + "TableNameHere");
onCreate(sqLiteDatabase);
}
For more information of how to implement SQLite please read this.
Please review the following types in order to create your table:
1.0 Storage Classes and Datatypes
Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:
NULL. The value is a NULL value.
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
BLOB. The value is a blob of data, stored exactly as it was input.
For more information of datatypes please see this link.
So your table should be create like this:
String CREATE_TableName_TABLE = "CREATE TABLE TableNames (Id INTEGER PRIMARY KEY AUTOINCREMENT, TagNo TEXT,
Description TEXT, WeeksGone INTEGER NOT NULL DEFAULT 0 );";
I am working on an android application that uses two databases. Recently, I had to add a new column to one of the databases. Upon doing so, it broke my database. Installing and re-installing the application on my device did nothing, and I also updated the DB version.
Trying to insert data will net me this error:
E/SQLiteLog﹕ (1) table message_table has no column named msg_type
So, I tried taking out the "msg_type" column from the insert, and inserting data which gave me this error:
E/SQLiteLog﹕ (1299) abort at 8 in [INSERT INTO message_table(recipient,message) VALUES (?,?)]: NOT NULL constraint failed: message_table.msg_typeTEXT
Here is the oncreate:
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + //msg_id
COL_2 + " TEXT NOT NULL, " + //recipient
COL_3 + " TEXT, " + //message
COL_4 + "TEXT NOT NULL);"); //message type
}
and the insert class:
public boolean addMessage(String recipient, String message, String type){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//populate message object
contentValues.put(COL_2, recipient);
contentValues.put(COL_3, message);
contentValues.put(COL_4, type);
//insert new message
long result = db.insert(TABLE_NAME, null, contentValues);
//check if operation was successful
if(result == -1)
return false;
else
return true;
}
How can I be getting an error for either case? I thought that it didn't recognize the new column was added from the first error, but it also doesn't like not receiving the data for that column.
The error is happening because there is no space between the column name and the TEXT. So the column name becomes message_table.msg_typeTEXT:
COL_4 + "TEXT NOT NULL);"); //message type
This should fix the error:
COL_4 + " TEXT NOT NULL);"); //message type
I have the following two tables:
Orders
_id
customer
note
ready
OrderTransactions
_id
orderid(links to orders._id)
product
quantity
What I want to do is delete all OrderTransactions that have an order where ready is equal to "y"
I've seen the join mysql statements to do this, but I don't know how to implement those joins in the mDb.delete statement below
public OrdersDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
private static final String ORDERS_CREATE =
"create table orders (_id integer primary key autoincrement, "
+ "customer text not null, note text not null, export text not null);" ;
private static final String ORDERPRODUCTS_CREATE =
"create table orderproducts (_id integer primary key autoincrement, "
+ "orderid integer not null, product text not null, quantity text not null);";
public boolean deleteOrderProductsAllReady(long orderId) {
return mDb.delete(DATABASE_TABLE_ORDERPRODUCTS, ***criteria***, null) > 0;
}
I think the query should look like this:
DELETE ordtran
FROM OrderProducts AS ordtran
INNER JOIN Orders AS ord
ON ordtran.orderid = ord._id
WHERE [ready="y"]
but I don't know how to put that into the above mDb.delete syntax.