Tolen
Tolen

Reputation: 111

Table not found issue in SQLite

I have written a code that get information from json file related to specific YouTube video and then stores the information I need in my database.

The parsing from json file has no problem. when I am trying to insert values in my database an error message appears telling me that no such table exists.

Here is the stack-trace:

 07-31 08:42:22.451: I/Database(365): sqlite returned: error code = 1,
   msg = no such table: youtube_VIDEOS 07-31 08:42:22.471: E/Database(365): 
   Error inserting video_CommentCount=70 video_CountView=50 video_Name=Badly 
   Drawn Boy - Disillusion (directed by Garth Jennings) 
   video_Url=https://www.youtube.com/watch?v=B11msns6wPU&feature=youtube_gdata_player 
   video_LIKES=60 video_Img=https://i1.ytimg.com/vi/B11msns6wPU/default.jpg
   video_Descrption=My new playlist Description 07-31 08:42:22.471: 
   E/Database(365): android.database.sqlite.SQLiteException:
   no such table: youtube_VIDEOS: , 
   while compiling: INSERT INTO youtube_VIDEOS(video_CommentCount, video_CountView,    video_Name, video_Url,video_LIKES, video_Img, video_Descrption) VALUES(?, ?, ?, ?, ?, ?, ?);
   07-31 08:42:22.471: E/Database(365): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)

And here is my database code:

package com.example.tstnetconnwithjson.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class youtube_db extends SQLiteOpenHelper {


    public static final String dataBase_NAME="YOUTUBE_database";
    private static final  int dataBase_VERSION=1;
    private static final  String dataBase_TABLE="youtube_VIDEOS";
    public static final String[] COLS_List={"video_Name","video_Descrption","video_Img","video_Url","video_CountView","video_LIKES","video_CommentCount"};

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //end of declaring attributes and tables conents
    public youtube_db(Context context) {
        super(context,dataBase_NAME, null, dataBase_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(
            "create table" + dataBase_NAME + "(" + COLS_List[0] +" text not null , "+ COLS_List[1]
                +" text not null , "+ COLS_List[2]+" text not null , "+COLS_List[3]+" text not null , "+COLS_List[4]+" integer , "+COLS_List[5]
                +" integer , "+COLS_List[6]+" integer ) ");             


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.i("in the upgrade", "ok");

    }

}

And here is the function that would insert the information in my database:

package com.example.tstnetconnwithjson.db;

import com.example.tstnetconnwithjson.tables.videos;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class youtubeDataBaseManager {

    SQLiteDatabase SQL_db;
    youtube_db my_Database;


    public youtubeDataBaseManager(Context c){

        my_Database=new youtube_db(c);
        SQL_db= my_Database.getWritableDatabase();

    }//end of costructor

    public long insert_Youtube_Info( videos video){
        ContentValues contentValues = new ContentValues(); 

        contentValues.put(youtube_db.COLS_List[0], video.getVideoname());
        contentValues.put(youtube_db.COLS_List[1], video.getDecscrption());
        contentValues.put(youtube_db.COLS_List[2], video.getImageurl());
        contentValues.put(youtube_db.COLS_List[3], video.getVediourl());
        contentValues.put(youtube_db.COLS_List[4], "50");
        contentValues.put(youtube_db.COLS_List[5], "60");
        contentValues.put(youtube_db.COLS_List[6], "70");

        long addResult ;
        addResult= SQL_db.insert(youtube_db.dataBase_TABLE, null, contentValues); 


        if(addResult==-1)
        {
            Log.i("add video", "add error....  ");

        }
        else
        {
            Log.i("add video", "add:ok....  ");
        }
        return  addResult;



    }

Can anyone tell me what is the problem?

Upvotes: 0

Views: 117

Answers (3)

Ken Wolf
Ken Wolf

Reputation: 23269

"create table" + dataBase_NAME + "(" 

You are missing space between name and <create table> keyword. You have to change it to:

"create table " + dataBase_TABLE + "("

Otherwise, your DDL statement won't work.

Later, you try and reference a table called "youtube_VIDEOS", which doesn't exist. Because you never created it. It's all about typo.

Upvotes: 4

Shani Goriwal
Shani Goriwal

Reputation: 2104

you should change here:

create table" + dataBase_TABLE+ "(" + COLS_List[0] +" text not null , "+ COLS_List[1]
            +" text not null , "+ COLS_List[2]+" text not null , "+COLS_List[3]+" text not null , "+COLS_List[4]+" integer , "+COLS_List[5]
            +" integer , "+COLS_List[6]+" integer ) ");

Upvotes: 2

TN888
TN888

Reputation: 7739

You have that error, because your table in database didn't successfully created. Why ? Because your table hasn't got primary key (id). Add, e.g :

`_id` INT PRIMARY KEY AUTOINCREMENT,

And now every thing should work correctly

Upvotes: 1

Related Questions