0

I have created a Databasehelper.java class and in that class I have called SQLite database class and implemented all its methods and in the main class when I am calling this class instance and when I am running the app It is showing "Unfortunately your app has stopped"

this is code

package com.example.android.viewpager1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Vikas on 5/1/2016.
 */
public class Databasehelper extends SQLiteOpenHelper
{
public static final String DATABASE_NAME="Answers.db";
    public static final String TABLE_NAME="answers_table";
    public static final String COL_1="QUESTION_NO";
    public static final String COL_2="QUESTION_ANS";



    public Databasehelper(Context context)
    {
        super(context,DATABASE_NAME,null,1);
        SQLiteDatabase db= this.getWritableDatabase();
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
    db.execSQL("create table" + TABLE_NAME+"(QUESTION_NO INTEGER PRIMARY KEY AUTOINCREMENT,QUESTION_ANS VARCHAR);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
      db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
      onCreate(db);
    }
}

and this is mainactivity.java

package com.example.android.viewpager1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;

import java.util.List;
import java.util.Vector;

public class MainActivity extends FragmentActivity
{
    Databasehelper mydb;

    private PagerAdapter mPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_layout);
        initialisePaging();
        mydb=new Databasehelper(this);//here I am making DB 

    }

here I am making database but it is not running and I also know that on the main thread or UI thread we should not create database or networking object and fro that we should use AsynTask class but can anyone tell how to use it or any other solution the logcat are

05-02 01:34:29.710 32328-32328/com.example.android.viewpager1 E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.viewpager1/com.example.android.viewpager1.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEanswers_table": syntax error (code 1): , while compiling: CREATE TABLEanswers_table (QUESTION_NO INTEGER PRIMARY KEY AUTOINCREMENT,QUESTION_ANS VARCHAR); at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2141) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2166) at android.app.ActivityThread.access$700(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5019) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:807) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:574) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteException: near "TABLEanswers_table": syntax error (code 1): , while compiling: CREATE TABLEanswers_table (QUESTION_NO INTEGER PRIMARY KEY AUTOINCREMENT,QUESTION_ANS VARCHAR); at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594) at com.example.android.viewpager1.Databasehelper.onCreate(Databasehelper.java:27) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) at com.example.android.viewpager1.Databasehelper.(Databasehelper.java:22) at com.example.android.viewpager1.MainActivity.onCreate(MainActivity.java:24) at android.app.Activity.performCreate(Activity.java:5056) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2099) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2166)  at android.app.ActivityThread.access$700(ActivityThread.java:141)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:5019)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:511)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:807)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:574)  at dalvik.system.NativeStart.main(Native Method) 

6
  • Why create a DB in your app? Deploy the pre-created DB it with your app. Commented May 1, 2016 at 19:26
  • I have to use SQLite DB only Commented May 1, 2016 at 19:35
  • I have used Sqlite DB only which is provided by android only Commented May 1, 2016 at 19:35
  • You can create a SQLite DB and deploy it with your app by putting it in the ASSETS folder of your android project Commented May 1, 2016 at 19:43
  • @VikasKumar try reading the logcat it points out valuable information for debug Commented May 1, 2016 at 19:46

2 Answers 2

1

There are some syntax problems in your code that I will point out but fixing them doesn't guarantee your app works, you still need to read Logcat for further information:

1- db.execSQL("create table " + TABLE_NAME+" ("+ COL_1 +" INTEGER PRIMARY KEY AUTOINCREMENT, "+COL_2+" VARCHAR);");

(note the added spaces after "table" and before "(" and also using your columns constants instead of hard coded strings)

2- db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);

(note for the added spaces after EXISTS)

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to give space between 'TABLE_NAME' and '('. See the updated code. Let me know in case of any query.

public void onCreate(SQLiteDatabase db)
    {
    db.execSQL("create table" + TABLE_NAME+" ( _id INTEGER PRIMARY KEY AUTOINCREMENT,QUESTION_NO INTEGER  AUTOINCREMENT,QUESTION_ANS VARCHAR);");
    }

1 Comment

that does not solved the problem still it is not running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.