I'm newbie to android. I have gone through the android docs and I googled it, but I'm confused why my code is returning a no such table error.
I want to create a DB named as demo and I want to create a table student with three columns.
Here's my code:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String dbName="demo";
public DatabaseHelper(Context context) {
super(context, dbName, null, 1);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table IF NOT EXISTS student(title text,address text,gender text)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insert_datas(String title,String artist,String patharray,String table_name){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put("title",title);
initialValues.put("address",address);
initialValues.put("gender",gender);
long n = db.insert(table_name,title,initialValues);
System.out.println("n"+n);
db.close();
}
public Cursor get_datas(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor cur=db.rawQuery("SELECT * FROM student",null);
return cur;
}
}
and this how i m call the DatabaseHelper class:
public class MYActivity extends Activity{
DatabaseHelper dbAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customlist);
dbAdapter = new DatabaseHelper(this);
dbAdapter.insert_datas("test","test","test","student");
}
}
My error
**11-26 02:10:00.210: INFO/SqliteDatabaseCpp(27151): sqlite returned: error code = 1, msg = no such table: student, db=/data/data/com.player.activites/databases/demo
11-26 02:10:00.210: ERROR/SQLiteDatabase(27151): android.database.sqlite.SQLiteException: no such table: student: , while compiling: INSERT INTO studenty(title,address,gender) VALUES (?,?,?)**
Thanks
Try upgrading the database version number (right now you have it as 1). Is it possible you ran your app and tried to access the DB before your helper's onCreate actually did anything? If you did that, you would have an empty DB at version 1. If you don't want to increment your version numbers, you could alternatively uninstall your app or clear its data to delete the DB file entirely.
Related
i have a very simple code i am trying to create SQL lite database but it's not creating while i check my DEVICE FILE EXPLORER folder there is not any database folder there under my apps folder.
here is the code:
DatabaseHelper.java
package com.example.ali.schoolapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db = this.getWritableDatabase();
db.execSQL("CREATE TABLE Items " + " (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, description TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
MainActivity.java
package com.example.ali.schoolapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
}
}
The database will not get created until you attempt to do something with it. Only then (when getWritableDatabase or getReadableDatabase is called) will the database be created.
So you could try :-
myDb = new DatabaseHelper(this); //<<<<<<<<<<< EXISTING LINE
Cursor csr = myDB.getWritableDatabase().query("Items",null,null,null,null,null,null);
csr.close();
And then the Database would have been created.
Note the above isn't how you would typically access the database, it's a quick fix. Normally you'd have you access methods in a Class (perhaps in the DatabaseHelper Class).
P.S. your onUpgrade method would very likely fail if you changed the version number (4th parameter (which is 1) when you call super). That is onUpgrade calls onCreate as the table exists you will get an error. You would typically DROP the table before calling onCreate).
I have the following Java Class
package com.sjhdevelopment.shaunharrison.myejuiceapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "EJuiceData.db";
public static final int DATABASE_VERSION = 1;
public MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase database) {
database.execSQL("create table if not exists Inventory");
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
database.execSQL("ALTER TABLE Recipe ADD COLUMN NOTES TEXT");
onCreate(database);
}
}
Then in my main activity I have the following;
public class Calculation extends AppCompatActivity {
private MyDBHelper dbHelper;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation);
try {
dbHelper = new MyDBHelper(this);
dbHelper.getWritableDatabase();
}
catch(Exception e)
{
showError("Error", e.getMessage());
}
}
However, when I debug the code to see if the database + tables are being created with a break point on the try and on the database.execSQL("create....
There debugger doesn't actually go into the MyDBHelper class to do anything, therefore I'm assuming that the database + tables aren't being created
I've looked online and it says that when a getWritableDatabase/readable is called the onCreate method should get called
Any idea's as to what I'm doing wrong?
onCreate(...) is only called when the database is created for the first time. If you want to change your database you have to increase DATABASE_VERSION then it will jump into onUpgrade(...)
It looks like your SQL is not correct onCreate. Instead of database.execSQL("create table if not exists Inventory"); it should be
database.execSQL("CREATE TABLE table_name
(id_column INTEGER PRIMARY KEY
, column_name TEXT);";
You need to manually created every table with all the corrected columns. They will be created once you call the database for the first time.
I am new to Android Programming.. I am trying to create sqLite Database in android Here is my code
sqLitee.java
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class sqLitee extends SQLiteOpenHelper
{
public static final String DB_NAME="student.db";
public static final String TABLE_NAME="student";
public static final String COL_1="ID";
public static final String COL_2="FIRST_NAME";
public static final String COL_3="LAST_NAME";
public static final String COL_4="MARKS";
public sqLitee(Context context)
{
super(context, DB_NAME,null, 1);
// TODO Auto-generated constructor stub
SQLiteDatabase db=this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTO INCREMENT, FIRST_NAME TEXT, LAST_NAME TEXT, MARKS INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
onCreate(db);
}
}
MainActivity.java
package com.example.sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
sqLitee myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDB = new sqLitee(this);
setContentView(R.layout.activity_main);
}
}
When I am trying to run it on Android virtual Device , It says unfortunately , sqLite has stopped..
Thanks
remove this line in your sqllite constructor:
SQLiteDatabase db=this.getWritableDatabase();
you try to get the db, which is currently initializing.
There's a syntax error in your CREATE TABLE SQL. AUTO INCREMENT should be AUTOINCREMENT.
As always, first see the logcat for exception stacktrace and to learn what is failing and where.
i try to access a database of another application, i want to insert data on that database from my application. I test it on my phone (rooted) but i get errors. Can anyone help me?
12-30 00:01:27.315: E/SQLiteLog(10477): (14) os_unix.c:30241: (13) open(/data/data/com.otherapplication/databases/records.db) -
12-30 00:01:27.376: E/SQLiteDatabase(10477): Failed to open database '/data/data/com.otherapplication/databases/records.db'.
12-30 00:01:27.376: E/SQLiteDatabase(10477): at com.test.MySQLiteOpenHelper.insertData(MySQLiteOpenHelper.java:28)
12-30 00:01:27.376: E/SQLiteDatabase(10477): at com.test.MainActivity.test(MainActivity.java:45)
12-30 00:01:27.446: E/AndroidRuntime(10477): at com.test.MySQLiteOpenHelper.insertData(MySQLiteOpenHelper.java:28)
12-30 00:01:27.446: E/AndroidRuntime(10477): at com.test.MainActivity.test(MainActivity.java:45)
Here are the files, MySQLiteOpenHelper.java :
package com.test;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
MySQLiteOpenHelper(Context context) {
super(context, "/data/data/com.otherapplication/databases/records.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insertData() {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", "testvalue");
database.insert("table", null, values);
database.close();
}
}
MainActivity.java
public class MainActivity extends Activity {
MySQLiteOpenHelper controller = new MySQLiteOpenHelper(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test(View v) {
controller.insertData();
}
}
I have found the solution:
I did chmod 777 to that application's 'databases' folder and that database file. And now i can insert data from my application.
I have four EditText and two buttons namely, save and review button.
If i press the save button EditText value is stored in to the database and on clicking the review button saved value in the database is listed in ListView.
How is this done please anybody explain to me.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "MedicinePill", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS names3 ("
+ BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, first VARCHAR, last VARCHAR ,dose2 VARCHAR ,dose3 VARCHAR)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Steps to upgrade the database for the new version ...
}
}
//this is my database class in this where i add the insert data from the database
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Medical extends Activity
{
Button AddMedicine,ReviewMedicine;
EditText dose1,dose2,dose3;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddMedicine = (Button)findViewById(R.id.add);
ReviewMedicine = (Button)findViewById(R.id.rev);
dose1 = (EditText)findViewById(R.id.Edit1);
AddMedicine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String s1 = dose1.getText().toString();
// TODO Auto-generated method stub
}
});
}
}
//and this is my activity class
how i add edittext value in to database pls any body help me!!!!!!!!
You need to create class insertdose in DatabaseHelper like this:
public long insertdose(String dose1)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_dose, dose1);
return db.insert(DATABASE_TABLE, null, initialValues);
}
and call it at your onClick view
You will need to create a method in the DatabaseHelper class that takes in four strings and adds them to your database.
Then in your onClick() method in your Medical activity class you can get the string values from your 4 EditText fields and call your method with these values.
I would recommend going through the Notepad Tutorial. This should provide a good example of what you want to do.