I'm having trouble with a SELECT statement in my SQLite Database. When the part of the app relating to this SELECT statement is ran, the app closes. Below you will find a picture of my Database.
My SQLite Database Schema
Each time an expense is recorded the expense of course goes into the "expenses" table but it also goes into the "peoplexpenses" table. The expense when created, is entered multiple times into the "peoplexpenses" table as more than 1 person can be related to an expense (hence why the "peoplexpenses" table is there in the first place to prevent a Many-to-Many relationship). So what I'm trying to get is, the person's name along with all their related expenses added together to give a total for the person. Hope that makes sense.
My failing SQL statement is:
"SELECT P.name, sum(E.amount)
FROM " + TABLE_EXPENSES + " E, " + TABLE_PEOPLE + " P, " + TABLE_PEOPLEXPENSES + "PE
WHERE PE.pid = P.pid AND PE.eid = E.eid
GROUP BY P.name"
Please find the Create Table statements and new LogCat here: https://pastebin.com/8eP1BBCt
Related
I am trying to insert data in database but it is giving me following error.
table places has no column named PLACE_NAME (code 1): , while compiling: INSERT INTO places(PLACE_NAME,IS_SELECTED,placeID,LONGITUDE,LATITUDE) VALUES (?,?,?,?,?)
here is how I am creating my Database Table
// Create a table to hold the places data
final String SQL_CREATE_PLACES_TABLE = "CREATE TABLE IF NOT EXISTS " + PlaceContract.PlaceEntry.TABLE_NAME + " (" +
PlaceContract.PlaceEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + PlaceContract.PlaceEntry.COLUMN_PLACE_NAME + " VARCHAR, " +
PlaceContract.PlaceEntry.COLUMN_PLACE_LATITUDE + " VARCHAR, " + PlaceContract.PlaceEntry.COLUMN_PLACE_LONGITUDE + " VARCHAR, "+
PlaceContract.PlaceEntry.COLUMN_PLACE_IS_SELECTED + " VARCHAR, " +
PlaceContract.PlaceEntry.COLUMN_PLACE_ID + " TEXT NOT NULL, " +
"UNIQUE (" + PlaceContract.PlaceEntry.COLUMN_PLACE_ID + ") ON CONFLICT REPLACE" +
"); ";
please help me what I am doing wrong ....
The most common causes of column not found are typing errors and a misconception in regards to the onCreate method.
The former is unlikely if you are consistently using a single source for the column name e.g. if you use PlaceContract.PlaceEntry.COLUMN_PLACE_NAME to refer to the place_name column.
With the latter, the onCreate method only runs automatically when the database is created, any changes made to the schema, such as adding columns, will not be applied. Thus if you changed the CREATE SQL string to add the PLACE_NAME column that column will not be added.
When developing an App and when the data can be lost then then there are three quick ways to rectify the situation.
Delete the App's data and rerun (the database will be deleted (unless the database is stored outside of the App (not recommended and not the typical scenario))).
Uninstall the App and rerun (also delete's the App's data).
IF the onUpgrade will drop the said table or tables and then recreate the tables (generally by calling the onCreate method) then the database version can be increased (this is the 4th parameter of the super call when constructing the Database Helper class (i.e. the class that extends SQLiteOpenHelper)).
If the data in the database cannot be lost, then the alternative is to use the ALTER to add the column or to create another table, copy the data from the original table and then drop the original table and use ALTER to rename the new table to be the original table.
I've a requirement in my android app.
There is an already existing database A which has a table T. From the next release of the app, I want to create a new database B and MOVE my table from A to B WITH all the existing data.
I am guessing there'd be some way to take dump from the existing db and store that in an asset file. And then use that file to restore the table in new db.
Can't store that data in-memory as it might go to inconsistent state if the app is killed in between.
I am not able to find any well-defined way to do this after spending some time searching in the docs/on the forums.
Please help.
You can achieve this by using ATTACH command of sqlite. First is by specifying the path of the first database.
private static String FIRST_DB_PATH = context.getDatabasePath("Sample.sqlite").toString();
Then you attach it to secondDb
SQLiteDatabase secondDB = secondDBHandler.getWritableDatabase();
secondDB.execSQL("ATTACH DATABASE '" + FIRST_DB_PATH + "' AS tempDb");
Then do the insert query. NOTE: You use secondDB as main
secondDB.execSQL("INSERT INTO main." + SeconDB_table_name + "SELECT * FROM tempDb."+ FirstDB_table_name );
Then finally detach the first db
secondDB.execSQL("DETACH tempDb");
Hope this helps
EDIT:
Do this for dropping table
secondDB.execSQL("DROP TABLE IF EXISTS main." + SeconDB_table_name);
Ok heres how to create a table for secondDB as a copy of the firstDb
secondDB.execSQL("CREATE TABLE main." + SeconDB_table_name + " AS SELECT * FROM tempDb." + FirstDB_table_name);
If what you mean in comment is to drop the first table after moving it to second table, then drop it before dettaching
secondDB.execSQL("DROP TABLE IF EXISTS tempDb." + FirstDB_table_name);
I am programming on Android and only starting with SQL:
What I have are 2 SQL tables, table A contains a list of match-results of sports games, table B contains all information about the teams.
Table A has two team-ids, one for the home-team, one for the away-team.
I want to create a sql query, that gets a match-result for every match, that is linked up to the corresponding teams, e.g. the two team-ids in table A should get replaced by at least the team-name (preferably more columns) of table B.
So in short: For every match in table A -> get all match-info from table A -> add information for the home and the away team from table B, corresponding to home-id and away-id from table A -> deliver result
I achieved this party through an SQL JOIN, but I only managed to join the information for either the home- or the away-team, not both, since the columns get in conflict, since both the home and away team information come from the same table and thus the column names are the same (home team has "team_name", and away team also has "team_name" -> conflict)
How can I achieve this?
For some information on why I would like to do this in one query: I am working on Android, displaying a list of match-results, which are loaded asynchronously via a Loader, which feeds a Cursor to a CursorAdapter. As I understand the cursor, it's its nature to only deliver one result per row of the list, thus all the data querying has to be done in one sql query.
Thank you for your help!
EDIT: My current sql join is this
ScheduleTable.TABLE_SCHEDULE + " JOIN " + TeamsTable.TABLE_TEAMS + " ON " +
ScheduleTable.TABLE_SCHEDULE+"."+ScheduleTable.COLUMN_HOME_TEAM_ID + " = " + TeamsTable.TABLE_TEAMS+"."+TeamsTable.COLUMN_ID
The easiest way to look up a value from another table might be a subquery:
SELECT Date,
Result,
(SELECT Name FROM Teams WHERE ID = Schedule.HomeTeamID
) AS HomeTeamName,
(SELECT Name FROM Teams WHERE ID = Schedule.AwayTeamID
) AS AwayTeamName
FROM Schedule
However, this becomes unwieldy if you need to look up more than one column for a team.
To be able to address a single table that you are using multiple times in the same FROM clause, you must use table aliases:
SELECT Schedule.Date,
Schedule.Result,
HomeTeam.Name,
AwayTeam.Name
FROM Schedule
JOIN Teams AS HomeTeam ON Schedule.HomeTeamID = HomeTeam.ID
JOIN Teams AS AwayTeam ON Schedule.AwayTeamID = AwayTeam.ID
I'm using SQLite on Android using SQLiteDatabase (http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html)
I am developing a bible application, which has a single table with the following columns:
book : int
chapter : int
verse : int
wordIdx : int
strongId : string
word : string
each sentence is broken down in to a series of strongId/word pairs, so wordIdx is used to order the words, strongId is simply a index in to a concordance, and word is the word in the sentence.
so I have 300,000 rows
the bottleneck appears to be my query to get a list of words for each verse:
My SQL is effectively this:
SELECT strongId, word FROM ? WHERE book=? AND chapter=? AND verse=?
Here is the code:
Cursor cursor = mBible.database().rawQuery("SELECT " + KEY_STRONGID + "," + KEY_WORD + " FROM " + tableName() + " WHERE " + KEY_BOOK + "=? AND " + KEY_CHAPTER + "=? AND " + KEY_VERSE + "=?" , new String[] { String.valueOf(mChapter.mBook.index()), String.valueOf(mChapter.index()), String.valueOf(verse) });
cursor.moveToFirst();
mWordList = new ArrayList<Word>();
do {
mWordList.add(new Word(cursor.getString(1), cursor.getString(0)));
} while (cursor.moveToNext());
Now, I've tried putting each chapter in to its own temporary view (using CREATE TEMP VIEW) which cuts down the records to about 400 in my example how ever it is still taking far to long to query
Its taking of the order of 30 seconds to generate the text for two chapters to display to the user (using a temporary view and without using a temporary view). It takes about 5 seconds if I set up a dummy list of words to avoid the database query.
How can I improve the performance of this? It seems as if a temp view is having no impact on performance as I had hoped.
A view does not change the performance of a query; it just saves the query itself, not the results of the query.
If you open your database with the sqlite3 command-line tool on your desktop machine, you can use the EXPLAIN QUERY PLAN command to check how efficient your queries are.
Without any indexes, you query always scans the entire table:
> sqlite3 bible.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> EXPLAIN QUERY PLAN SELECT strongId, word FROM MyTable WHERE book=1 AND chapter=2 AND verse=3;
0|0|0|SCAN TABLE MyTable (~1000 rows)
With an index on your three lookup fields, SQLite can do a fast search in the index and needs to read only the matching records from the table:
sqlite> CREATE INDEX b_c_v ON MyTable(book, chapter, verse);
sqlite> EXPLAIN QUERY PLAN SELECT strongId, word FROM MyTable WHERE book=1 AND chapter=2 AND verse=3;
0|0|0|SEARCH TABLE MyTable USING INDEX b_c_v (book=? AND chapter=? AND verse=?) (~8 rows)
If you create a covering index (with all fields used in the query, lookup fields first), SQLite does not need to read from the table at all. However, this does not give a big speedup over a normal index, and might not be worth the additional storage cost:
sqlite> CREATE INDEX cov ON MyTable(book, chapter, verse, strongId, word);
sqlite> EXPLAIN QUERY PLAN SELECT strongId, word FROM MyTable WHERE book=1 AND chapter=2 AND verse=3;
0|0|0|SEARCH TABLE MyTable USING COVERING INDEX cov (book=? AND chapter=? AND verse=?) (~8 rows)
Please note that SQLite can use at most one index per table in a query, so it does not always make sense to create multiple indexes.
Use EXPLAIN QUERY PLAN to check which indexes are actually used, and whether you can create a few indexes to optimize most of your queries.
Also see the Query Planning documentation.
I ended up creating temporary tables and performance is now acceptable
My app reads an XML file on the internet, takes note of the time and creates/writes an SQLite database. The next time data is required, if the time is >24hrs the database is updated (xml downloaded again).
The problem is that whenever I relaunch the app in AVD it has to re-download and so I notice that all the data in the database is written again (duplicated). So instead of 10 items, I have 20 (10+10 duplicates). If I relaunch again I get another 10 items duplicated.
I thought about how I could prevent the duplication of the database (or delete the old entries), so I decided to increment the database version every time the content is downloaded. I thought this would trigger the onUpgrade() method so the data would be cleared but nothing changes.
Now I am clueless. How should I go about this?
On your database create you'll want to use the UNIQUE constraint. You may not want the ON CONFLICT REPLACE that i use, but you should get the idea.
For Ex:
private static final String DATABASE_CREATE_NEWS= "create table news (_id integer primary key autoincrement, "title text not null, description text not null, date text not null, LastModified text not null, UNIQUE(title, date) ON CONFLICT REPLACE);";
Here is another solid thread that talks about it as well.
SQLite table constraint - unique on multiple columns
Here is some more info on the android sqlite: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
You should create an index on the columns that represent a unique identifier.
see this article on SQLite's website.
CREATE INDEX ix_tblexample ON TableName ( Column1, Column2, Column3 [, Column4, etc..])
Or (as per your comment) you can select the table into a cursor and check for each one.
String sql = "select * from " + tableName + "where column1 = " + param1 + "and column2 = " + param2;
Cursor cur = _db.rawQuery( sql, new String[0] );
if(cur.getCount() == 0)
{
//upload
}