I would fully translate my Android app. (this includes the SQLite is displayed on the phone language)
This is like now connect;
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "quotes.db";
private static final String DB_PATH_SUFFIX = "/databases/";
private static final String TABLE_QUOTES = "quote";
private static final String KEY_ID = "_id";
static Context myContext;
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
myContext = context;
}
I had thought to remove the name string database and pass it the name database using the strings.xml file.
super(context, context.getResources (). getString (R.string.DATABASE_NAME), null, DATABASE_VERSION);
Also look for the query to pass on through strings.xml, but can not find clear documentation.
I would appreciate if I do not guide a little. Many Thanks.
Example the query:
// Select All Query
String selectQuery = "SELECT name, COUNT(author_name ) AS count FROM author LEFT JOIN quote ON name = author_name WHERE name LIKE '%"
+ value + "%' GROUP BY name ORDER BY name ASC";
I modifing the table sqlite and code adding "+Locale.getDefault().getLanguage() +":
String selectQuery = "SELECT quote._id, quote.author_name_"+ Locale.getDefault().getLanguage() +", quote.qte_"+ Locale.getDefault().getLanguage() +
", quote.category_name_"+ Locale.getDefault().getLanguage() +",fav FROM quote,author where author.name_"+ Locale.getDefault().getLanguage()+
" = quote.author_name_"+ Locale.getDefault().getLanguage() +" and quote.category_name_"+ Locale.getDefault().getLanguage() +
"!='Tips Romanticos' ORDER BY quote.author_name_"+ Locale.getDefault().getLanguage() +" "+limit;
The query result is:
SELECT quote._id, quote.author_name_es, quote.qte_es,
quote.category_name_es,fav FROM quote,author where author.name_es =
quote.author_name_es and quote.category_name_es!='Tips Romanticos'
ORDER BY quote.author_name_es LIMIT 50
Related
I am developing a dictionary app. I have the following code which show 20 most recent history word.
public List<Bean> getHistoryWords() {
SQLiteDatabase db = initializer.getReadableDatabase();
String sql = "SELECT * FROM " + HISTORY_NAME +
" WHERE " + STATUS + " ORDER BY " + STATUS + " DESC LIMIT 20" ;
Cursor cursor = db.rawQuery(sql, null);
List<Bean> wordList = new ArrayList<Bean>();
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String english = cursor.getString(1);
String bangla = cursor.getString(2);
String status = cursor.getString(3);
wordList.add(new Bean(id, english, bangla, status));
}
cursor.close();
db.close();
return wordList;
}
I would like to provide an option for user to change the number of history item from LIMIT 20 to LIMIT 50 or LIMIT 100 through Preferences.
I follow this tutorial, but I was stuck in "5) Saving/reading data". I tried to put the SharedPreferences under getHistoryWords, but I got error cannot resolve getBaseContext.
public List<Bean> getHistoryWords() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Where should I put the code?
There is a simplest way for it.
First, create YourAppName , that extends Application
Declare Context as static and init it on Application 's onCreate method,
public YourApp extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
YourApp.context = getApplicationContext();
}
public static Context getAppContext() {
return YourApp.context;
}
}
declare YourApp in Manifest.xml
android:name="com.yourpackagename.app.YourApp" in application tag,
Now, you can get for Context, every where you need like this,
YourApp.getAppContext()
I hope this will help for you.
I think I have checked all the previous posts on this issue but none seem to be helping me...
Basic stuff - trying to use a cursor adaptor bound to my database but I am getting:
java.lang.IllegalArgumentException: column '_id' does not exist
My schema has that column defined (although I do have 2 tables, both with the same name - is that an issue?), like this:
public static abstract class dbMain implements BaseColumns {
public static final String TABLE_NAME = "mpgMain";
public static final String ENTRY_ID = "_id";
public static final String VEHICLE_NAME = "v_name";
}
public static abstract class dbHistory implements BaseColumns {
public static final String TABLE_NAME = "mpgHistory";
public static final String ENTRY_ID = "_id";
public static final String TRIP_DATE = "date";
}
From other posts, I have put _id in my cursor porjection:
String[] projection = {dbMain.ENTRY_ID, dbMain.VEHICLE_NAME};
Cursor mpgCur = mpgDB.query(dbMain.TABLE_NAME, projection, null, null, null, null, null);
...but then my stack trace gives the same error, on the query line. If I remove the ENTRY_ID from the projection, the same error but on the later line instantiating the adaptor:
String[] fromColumns = {dbMain.VEHICLE_NAME};
int[] toViews = {R.id.displayVehicle};
SimpleCursorAdapter vehAdapter = new SimpleCursorAdapter(this,
R.layout.activity_first_screen, mpgCur, fromColumns, toViews, 0); <--- HERE
I've also tried putting a space in the CREATE TABLE command, before the _id field:
private static final String SQL_CREATE_MAIN_TABLE =
"CREATE TABLE " + dbMain.TABLE_NAME + " ( " +
dbMain.ENTRY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Any ideas gratefully received...
If I'm not mistaken, by implementing BaseColumns, you don't have to specify "_id" in your schema. Just use the constant _ID (so, dbMain._ID and dbHistory._ID)
I downloaded a simple login page code in zip file from
http://bit.ly/saket_github_loginpage.
Please check this application and tell me where the database file is. And how can I see this? I also want to create some new tables. How can I do this? I have tried getting it to work with the following code:
public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "saket.db";
private static final int DATABASE_VERSION = 1;
public static final String SAKET_TABLE_NAME = "login";
public static final String SAKET_TABLE_NAME1 = "Employee";
private static final String SAKET_TABLE_CREATE =
"CREATE TABLE " + SAKET_TABLE_NAME + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NO TNULL);";
private static final String SAKET_TABLE_CREATE1 =
"CREATE TABLE " + SAKET_TABLE_NAME1 + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"name TEXT NOT NULL, Address TEXT NOT NULL, email TEXT NOT NULL);";
private static final String SAKET_DB_ADMIN = "INSERT INTO" +
SAKET_TABLE_NAME+"values(1, admin, password, admin#gmail.com);";
private static final String SAKET_DB_ADMIN1 = "INSERT INTO " +
SAKET_TABLE_NAME+"values(2, emp, password, Emp#gmail.com);";
private static final String SAKET_DB_ADMIN2 = "INSERT INTO " +
SAKET_TABLE_NAME1 + "values(1, mahesh, address, mahesh#gmail.com);";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}
}
It's better you check on emulator, or a rooted device....... Follow these steps
In emulator run your application.
In eclipse go to Window>Show view>Other...>Android and click file explorer
In File explorer find a folder called data which should not be on your SD card
in data>data>your.application.packagename you can find database folder, your database is there
Find an icon called pull a file from device...... you can copy that database anywhere you like using this button....
There are many clients to view and operate the SQLITE database including firefox has one..... I recommend to use this.... If you are on ubuntu they have a good application in their software center..... [I don't remember the name so apologies for that]
I hope this works for you....
1)You can see your database in file explorer. you can refer to file explorer as
Window -> show view -> other -> Select File Explorer.
in File Explorer goto
data -> data -> com.YourApplicationPackage -> YourDatabase
2)To create new table is by SQL use CREATE query having stored in String Constant and the execute it by your SQLite database object. if your sqlite database object is db then it would be db.execSQL(StringConstant); String constant carries your SQL CREATE query...
I hope you got your answer....
I am trying to put together an SQL database but don't really know how to make it work. The intention is to have multiple columns, some with integers, some with strings in their cells. For this app, I want repetitions to be an integer and exercise to be a string. Here is the relevant parts of the code:
public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_EXERCISE = "exercise";
public static final String KEY_REPS = "repetitions";
private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_DATE + " text not null, "
+ KEY_EXERCISE + " text not null, "
+ KEY_REPS + " int not null, "
public long createExercise(String exercise, int reps) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_EXERCISE, exercise);
initialValues.put(KEY_REPS, reps);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
I put data in this table using test strings. Then I try to pull the data with the following query:
public Cursor graphQuery(String exercise, String workout) {
return mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS}, null, null,
null, null, null);
From there I try to put the data into a number array but it gives me an error. It tells me to put KEY_REPS as a number when I declared it. But if I declare KEY_REPS as a number it doesn't let me build my databes.
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
startManagingCursor(cursor);
Number[] reps = new Number[]{workoutDbAdapter.KEY_REPS}; //error here
I feel like I am missing a key part in how to create my database. Can anyone help?
Code from book I am trying to follow (except using integers) (from comment on first answer)
private void fillData() {
Cursor remindersCursor = mDbHelper.fetchAllReminders();
startManagingCursor(remindersCursor);
// Create an array to specify the fields we want (only the TITLE)
String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};
That being said, if someone knows of a good website that teaches SQLite as it applies to Android that would be awesome. The only ones I have been able to find are generic SQL sites and they aren't very helpful.
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
startManagingCursor(cursor);
Number[] reps = new Number[]{WorkoutDbAdapter.KEY_REPS}; //error here
This code here doesn't do what (I think) you want it to. You need to iterate over the cursor and get the data from there. I'm pretty sure, if you followed the Android sample code for using databases that WorkoutDbAdapter.KEY_REPS is a string constant that holds reps column name.
Try doing something like this:
List<Number> allReps = new ArrayList<Number>();
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
while (cursor.moveToNext()) {
int reps = cursor.getInt(cursor.getColumnIndexOrThrow(mDbHelper.KEY_REPS));
allReps.add(reps);
}
Number[] repsArray = allReps.toArray(new Number[]{});
// do stuff with repsArray and don't forget to close cursor
i am trying to query my database so that it returns only rows with a specific value in a certain column.but when i run it, it still returns all the rows. please take a look at the code and see if i have done it incorrectly. I am trying to query for the string in the PRIORITY column. i want to display those specific rows in a listview. Thank you.
private static final String DATABASE_NAME = "MemoData.db";
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_TABLE = "Places";
public static final String KEY_ID = "_id";
public static final String NAME = "Title";
public static final String CATEGORY = "category";
public static final String PRIORITY = "priority";
private static final String TAG = "DBAdapter";
private static final String DATABASE_CREATE = "create table Places (_id integer primary key autoincrement, Title text, category text, priority text);";
public Cursor priorityData(){
String[] resultColumns = new String[] {KEY_ID,NAME,CATEGORY,PRIORITY };
String[] condition = {"High"};
Cursor cursor = db.query(DATABASE_TABLE, resultColumns, KEY_ID + "=" + "?", condition, null, null, null);
if(cursor.moveToNext()){
return cursor;
}
return cursor;
}
The values for priority columns can be "Low", "Medium" or "High". i am trying to query the database to return rows whose values are set as "High" in the PRIORITY column.
I am trying to query for the string in the PRIORITY column...
So your filter should be
PRIORITY + " = ?"
instead of
KEY_ID + "=" + "?"
Also it's not a good idea to have a text column to hold values from a limited set of possibilities that you are then filtering and your last if makes no sense.
I think he meant that you should use ENUM type to your PRIORITY variable, because it has limited set of possibilities.
So it should looks something like this:
priority ENUM('LOW','MEDIUM','HIGH') NOT NULL DEFAULT 'MEDIUM'