I have a listview with a custom adapter that uses an sql cursor. When I add items to my listview, the item will be added to my sqlite3 database and the listview will refresh.
My one problem is that I am able to add duplicate items, and I do not want my application to allow duplicates to be added.
I have added a primary key to my database (on itemNumber), however the database does not seem to be enforcing this. Here is how I create my database:
private static final String DB_CREATE_MASTER = "CREATE TABLE "
+ "MyTable"
+ " (_id INTEGER, itemNumber TEXT,"
+ "itemPlace TEXT," + "itemTimeTEXT,"
+ "itemCode INTEGER,"
+ "dbdatestamp TEXT" + "PRIMARY KEY(itemNumber)" +");";
Does anyone know why I am able to add more than 1 duplicate itemNumber to my listview?
Thanks!
EDIT:
private static final String DB_CREATE_MASTER = "CREATE TABLE "
+ "MyTable"
+ " (_id INTEGER PRIMARY KEY, itemNumber TEXT,"
+ "itemPlace TEXT," + "itemTimeTEXT,"
+ "itemCode INTEGER,"
+ "dbdatestamp TEXT" + "UNIQUE(itemNumber)" +");";
EDIT:
This is how I add the itemNumber to the database:
values.put("itemNumber", myClass.itemNumber);
values.put("itemName", myClass.itemName);
values.put("itemTime", myClass.itemTime);
values.put("dbdatestamp", "03/01/1960 08:55");
this.db.insert(MY_TABLE, null, values);
Your primary key consists of 2 fields: _id and itemNumber. That means that a duplicate itemNumber is not enough to enforce the constraint. A duplicate would be an item with the same _id AND itemNumber. You could change the CREATE statement like this
PRIMARY KEY(_id), UNIQUE(itemNumber)
The UNIQUE constraint wouldn't allow duplicate item numbers.
Related
I'm using JDBC in eclipse IDE , i want to put two foreign keys in my table 3 , one is referencing to the primary key in table 1 and one is referencing to the primary key in table 2. When i only put one foreign key constrains for any referencing table1 or table 2 , it works fine but when i include two it gives me sql exception as stated below:
java.sql.SQLSyntaxErrorException: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'foreign key(T2) references
Table2(T2) )' at line 1
String createString =
// TABLE 1
"CREATE TABLE " + this.tableName + " ( " +
"T1 varchar(50) NOT NULL PRIMARY KEY )";
// TABLE 2
"CREATE TABLE " + this.tableName + " ( " +`enter code here`
"T2 varchar(50) NOT NULL PRIMARY KEY )";
// TABLE 3
"CREATE TABLE " + this.tableName + " ( " +
"T1 varchar(50) " +
"T2 varchar(50) " +
"foreign key(T1) references Table1 (T1)" +
"foreign key(T2) references Table2(T2) )";
First, this is actually a MySQL question, unrelated to Java/JDBC. Secondly, and more importantly, you don't appear to be using the correct syntax, which would be...
CREATE TABLE TableName (
T1 varchar(50),
T2 varchar(50),
foreign key(T1) references Table1(T1),
foreign key(T2) references Table2(T2)
);
Formatted for your code, it would look like this:
String createString = "CREATE TABLE " + this.tableName + " ( " +
" T1 varchar(50)," +
" T2 varchar(50)," +
" foreign key(T1) references Table1(T1)," +
" foreign key(T2) references Table2(T2));";
You were missing commas after each item in the items list for your CREATE TABLE statement.
I am building an android app project with SQLite DB.
I got stuck on One-To-Many RelationShip.
This is One
private static final String createTableOrders =
"CREATE TABLE " + TABLE_ORDER + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
...
+ KEY_COLUMN_FORMATS + " INTEGER REFERENCES " + TABLE_FORMATS + "(" + KEY_ID + ")"
+ ");";
This is Many
private static final String createTableFormats =
"CREATE TABLE " + TABLE_FORMATS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
...
+ ");";
My problems is with set/get methods.
Lets say that I would need to get list of ids of all Formats that are in one Order.
I guess same thing goes for set method.
I tried to find this kind of question on SO but most of the question were just for SQL part.
P.S. Spent 3 hours trying to make it by myself but the only thing I got to was using GSON to code list of ids into String but it was just a dead-end.
EDIT: I need to get that information inside the code.
P.S.S I have been doing this for 18 hours so sorry if this is uber-stupid question.
A one-to-many relationship requires the foreign key column in the "many" table:
CREATE TABLE Orders (
ID PRIMARY KEY
);
CREATE TABLE Formats (
ID PRIMARY KEY,
OrderID REFERENCES Orders(ID)
);
To get all formats belonging to an order, you just look up rows with the ID of that order:
SELECT * FROM Formats WHERE OrderID = ?
In Jave:
Cursor cursor = db.query(TABLE_FORMATS,
new String[] { whatever columns you need },
"OrderID = " + orderID,
null, null, null, null, null);
while (cursor.moveToNext()) {
// read one order from the cursor
}
cursor.close();
First of all thanks for the help and the words of support.
As I am learning MySQL from the web I know I'm going to make many errors and thanks in advance for your patience and understanding.
I'm making the project based in Java which is then creating a MySQL Database and Tables.
Generally it is going well, until I have encountered this problem. It has given me the biggest headache so far. I read many articles on the error 150. I have read error 150 on MySQL site and I think that I have fulfilled the ten reasons why a error 150 occurs.
The call that I'm making is calling is creating each Table. Here are some of the Tables which are covering the problem that I'm having.
This is the WorkCalendar Table
private String workCalandar = "CREATE TABLE WorkCalendar ("
+ "WorkCalendarIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "WorkCalendarDate Date,"
+ "WorkCalendarDayCount INT(64),"
+ "WorkDayTypeIdNo INT(64),"
+ "PRIMARY KEY(WorkCalendarIdNo),"
+ "FOREIGN KEY(WorkDayTypeIdNo) REFERENCES WorkDayType(WorkDayTypeIdNo)"
+ ")";
This is the Department Table
private String department = "CREATE TABLE Department ("
+ "DeptIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "DeptName VARCHAR(25),"
+ "PRIMARY KEY(DeptIdNo)"
+ ")";
This is the Specialist Table
private String specialist = "CREATE TABLE Specialist ("
+ "SpecIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "PrefixIdNo INT(64),"
+ "SpecFirstName VARCHAR(30),"
+ "SpecSurname VARCHAR(35),"
+ "SpecDisplayName VARCHAR(72),"
+ "DeptIdNo INT(64),"
+ "PRIMARY KEY(SpecIdNo),"
+ "FOREIGN KEY(PrefixIdNo) REFERENCES Prefix(PrefixIdNo),"
+ "FOREIGN KEY(DeptIdNo) REFERENCES Department(DeptIdNo)"
+ ")";
This is the NewReferral Table
private String newReferral = "CREATE TABLE NewReferral("
+ "NewReferralIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "PatientNumber VARCHAR(12),"
+ "NewReferralDate Date,"
+ "DeptIdNo INT(64),"
+ "SpecIdNo INT(64),"
+ "NewReferralMatched BOOLEAN,"
+ "WorkCalendarIdNo INT(64),"
+ "PRIMARY KEY(NewReferralIdNo),"
+ "FOREIGN KEY(DeptIdNo) REFERENCES Department(DeptIdNo),"
+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
+ "FOREIGN KEY(WorkCalendarIdNo) REFERENCES WorkCalendar(WorkCalendarIdNo)"
+ ")";
The error is Can't create table 'basque30.newreferral' (errno: 150)
I have tracked the error down to the following line
+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
Could someone to point out the error of my ways and advise me how to resolve it.
Make sure the table referenced already exists.
Error no: 150 -- There is a wrong primary key reference in your code.
It's due to a reference FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo)
field does not exist.
As you mentioned
I have tracked the error down to the following line
+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
↑
Make sure table Specialist exists in order to refer as foreign key in another table.
Check the order of table creation.
Not related, but might help in future.
If you have admin permission on the server, you may want to start by running the MySQL command
SHOW INNODB STATUS
for MySQL 5.5
SHOW ENGINE INNODB STATUS
immediately after receiving the error. This command displays log info and error details.
I've got a DatabaseHelper class where I create the tables.
The first table (recordings) is working fine, but the second one is throwing an Exception when I try to work with it (Insert/Select...).
here is my code from the DatabaseHelper:
public void onCreate(SQLiteDatabase db) {
String createTable="CREATE TABLE recordings" +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
" name VARCHAR(255) NOT NULL," +
" datestart VARCHAR(255) NOT NULL" +
")";
String createTable2="CREATE TABLE recording_image" +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"imageurl VARCHAR(255) NOT NULL," +
"imagetime INTEGER NOT NULL," +
"prot_id INTEGER NOT NULL," +
"FOREIGN KEY(prot_id) REFERENCES recordings(_id) ON DELETE CASCADE" +
")";
db.execSQL(createTable);
db.execSQL(createTable2);
}
the Exception is:
no such table: recording_image.
What could cause this problem?
Yes, try to clear data of the application, and you can also look to the version of the db, If the version of the db you want to create are higher than the current db on the phone you have to delete and create again all the db.
please see : SQLiteOpenHelper
Hope that will help you :)
Problem solved. Just reinstalling the App or changing the Version number of the Database helped.
The first 2 go through without a hitch, but when I try to do the last one, I get this error message:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (corejava., CONSTRAINT #sql-8b8_50_ibfk_2 FOREIGN KEY (call_Number) REFERENCES book (call_Number))
There has got to be a way around this right?
Please see corrected code below: For any other newbies out there, my transaction call_Number INSERT field had incorrect values (tiny, minor, incorrect values!!!!)
s.executeUpdate (
"CREATE TABLE transaction ("
+ "patron_ID CHAR(10) NOT NULL,"
+ "call_Number CHAR(10) NOT NULL, check_Out_Date DATE NOT NULL, check_In_Date DATE NOT NULL,"
+ "PRIMARY KEY (patron_ID, call_Number),"
+ "overdue CHAR(15), total_Charge FLOAT)");
count3 = s.executeUpdate (
"INSERT INTO transaction"
+ " VALUES"
+ "('P222200000','MY.111.000','2011-06-06','2011-06-12','yes',5.00),"
+ "('P222200001','MY.111.001','2011-07-06','2011-07-12','no',5.00),"
+ "('P222200002','SF.111.002','2011-08-06','2011-08-12','yes',5.00),"
+ "('P222200003','SF.111.003','2011-09-06','2011-09-12','no',5.00),"
+ "('P222200004','AV.111.004','2011-10-06','2011-10-12','yes',5.00),"
+ "('P222200005','AV.111.005','2011-11-06','2011-11-12','no',5.00),"
+ "('P222200006','CO.111.006','2011-12-06','2011-12-12','yes',5.00),"
+ "('P222200007','CO.111.007','2011-01-06','2011-01-12','no',5.00),"
+ "('P222200008','IN.111.008','2011-02-06','2011-02-12','yes',5.00),"
s.executeUpdate(
"ALTER TABLE transaction ADD FOREIGN KEY (patron_ID) references " + dbName + ".patron (patron_ID)");
s.executeUpdate (
"ALTER TABLE patron ADD FOREIGN KEY (call_Number) references " + dbName + ".book (call_Number)");
s.executeUpdate (
"ALTER TABLE transaction ADD FOREIGN KEY (call_Number) references " + dbName + ".book (call_Number)");