0

我编写了一个代码,从 json 文件中获取与特定 YouTube 视频相关的信息,然后将我需要的信息存储在我的数据库中。

从json文件解析没有问题。当我试图在我的数据库中插入值时,会出现一条错误消息,告诉我不存在这样的表。

这是堆栈跟踪:

 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)

这是我的数据库代码:

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");

    }

}

这是在我的数据库中插入信息的函数:

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;



    }

谁能告诉我有什么问题?

4

3 回答 3

4
"create table" + dataBase_NAME + "(" 

您在名称和关键字之间缺少空格。<create table>您必须将其更改为:

"create table " + dataBase_TABLE + "("

否则,您的DDL声明将不起作用。

稍后,您尝试引用一个名为“youtube_VIDEOS”的表,但该表并不存在。因为你从来没有创造过它。这都是关于错字的。

于 2013-07-31T08:53:51.543 回答
2

你应该在这里改变:

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 ) ");
于 2013-07-31T08:55:35.377 回答
1

你有那个错误,因为你在数据库中的表没有成功创建。为什么 ?因为您的表没有主键(id)。添加,例如:

`_id` INT PRIMARY KEY AUTOINCREMENT,

现在每件事都应该正常工作

于 2013-07-31T08:52:37.537 回答