Android 在SQLite中找不到表问题
我编写了一段代码,从与特定YouTube视频相关的json文件中获取信息,然后将所需信息存储在数据库中 从json文件进行解析没有问题。当我试图在数据库中插入值时,出现一条错误消息,告诉我不存在这样的表 以下是堆栈跟踪:Android 在SQLite中找不到表问题,android,sqlite,Android,Sqlite,我编写了一段代码,从与特定YouTube视频相关的json文件中获取信息,然后将所需信息存储在数据库中 从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/Databas
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;
}
有人能告诉我问题出在哪里吗?您有这个错误,因为数据库中的表没有成功创建。为什么?因为您的表没有主键(id)。添加,例如:
`_id` INT PRIMARY KEY AUTOINCREMENT,
现在每件事都应该正常工作
"create table" + dataBase_NAME + "("
名称和关键字之间缺少空格。您必须将其更改为:
"create table " + dataBase_TABLE + "("
否则,您的DDL
语句将无法工作
稍后,您尝试引用一个名为“youtube_VIDEOS”的表,该表不存在。因为你从来没有创造过它。都是打字错误。您应该在这里更改:
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 ) ");
Tolen您在执行查询时犯了一个错误i已经更正了查询,但仍然存在相同的问题+1,我只是更新了您的问题(文本中的一些更改,以便更好地向初学者解释)。@:kenwolf:仍然存在相同的问题Tolen现在您需要清除应用程序数据。现在,错误可能是由于您创建了名为dataBase\u name的表,并试图在数据库中插入_TABLE@Tolen您是否清除了应用程序中的数据(来自设置/应用程序)?您需要这样做,或者完全卸载,然后重新安装。在执行此操作之前,表
create
语句只运行一次。它不是每次运行应用程序时都运行。@Shani Goriwal:仍然是同一个问题@:Ty221:仍然是同一个问题