我在一个数据库中有多个表(令牌和推文)。令牌表工作正常,我可以正确插入和更新内容,但我的tweets表抛出一个错误(SQLiteException接近“CREATE”)虽然它看起来我已正确输入所有内容。我尝试从手机中删除应用程序并重新开始,但它只是拒绝插入我的for循环中。
这是我的doInBackground方法,它从用户的时间轴获取推文并尝试将其存储在tweets表中: 列表responseList = twitter.getHomeTimeline(页面);
for (twitter4j.Status status : responseList)
{
int rt_amount = status.getRetweetCount();
String rt = Integer.toString(rt_amount);
String username = status.getUser().getScreenName();
String profile_image_url = status.getUser().getProfileImageURL();
String message = status.getText();
int favoriteCount = status.getFavoriteCount();
String favCount = Integer.toString(favoriteCount);
String name = status.getUser().getName();
String createdTime = status.getCreatedAt().toGMTString();
storeTweet.storeTweet(message, username, name, rt, createdTime, profile_image_url, favCount, "hi");
}
String CREATE_TWEETS_TABLE = "CREATE TABLE " + TABLE_TWEETS +
"("
+ T_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + T_MESSAGE + " TEXT,"
+ T_RETWEET_COUNT + " TEXT," + T_FAVORITE_COUNT + " TEXT," + T_USER + " TEXT,"
+ T_REAL_SCREENNAME + " TEXT," + T_MEDIA_URIS + " TEXT," + T_PROFILE_URL + " TEXT," + T_CREATED_TIME + " TEXT)";
的onCreate:
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(CREATE_TOKENS_TABLE);
database.execSQL(CREATE_TWEETS_TABLE);
}
addTweet:
public void addTweet(Tweet tweet)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(T_MESSAGE, tweet.getMessage());
values.put(T_FAVORITE_COUNT, tweet.getFavoriteCount());
values.put(T_RETWEET_COUNT, tweet.getRtCount());
values.put(T_USER, tweet.getUsername());
values.put(T_REAL_SCREENNAME, tweet.getRealName());
values.put(T_MEDIA_URIS, tweet.getMediaUris());
values.put(T_PROFILE_URL, tweet.getProfileImageUrl());
values.put(T_CREATED_TIME, tweet.getCreatedTime());
db.insertOrThrow(CREATE_TWEETS_TABLE, null, values);
db.close();
}
StoreTweet.java:
public class StoreTweet
{
private DBHelper db;
private Context context;
public StoreTweet(Context context){
this.context = context;
}
public void storeTweet(String message, String username, String realName, String rtCount, String createdTime,
String profileImageUrl, String favoriteCount, String mediaUris)
{
db = new DBHelper(context);
db.addTweet(new Tweet(message, username, realName, rtCount, createdTime, profileImageUrl, favoriteCount, mediaUris));
}
}
Stacktrace:
05-27 20:06:05.585: E/AndroidRuntime(26840): Caused by: android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: INSERT INTO CREATE TABLE tweets(_id INTEGER PRIMARY KEY AUTOINCREMENT,message TEXT,retweet_count TEXT,favorite_count TEXT,user TEXT,screenname TEXT,media_uris TEXT,profile_img_url TEXT,created_time TEXT)(message,profile_img_url,screenname,media_uris,retweet_count,favorite_count,created_time,user) VALUES (?,?,?,?,?,?,?,?)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
05-27 20:06:05.585: E/AndroidRuntime(26840): at util.DBHelper.addTweet(DBHelper.java:148)
05-27 20:06:05.585: E/AndroidRuntime(26840): at twitter.StoreTweet.storeTweet(StoreTweet.java:21)
05-27 20:06:05.585: E/AndroidRuntime(26840): at twitter.TWTimelineFrag$GetTwitterTimeline.doInBackground(TWTimelineFrag.java:127)
05-27 20:06:05.585: E/AndroidRuntime(26840): at twitter.TWTimelineFrag$GetTwitterTimeline.doInBackground(TWTimelineFrag.java:1)
05-27 20:06:05.585: E/AndroidRuntime(26840): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-27 20:06:05.585: E/AndroidRuntime(26840): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-27 20:06:05.585: E/AndroidRuntime(26840): ... 4 more
答案 0 :(得分:3)
您没有通过CREATE TABLE
执行insertOrThrow()
语句 - 即在您的SQL语句中添加INSERT INTO
。请改用execSQL()
。
答案 1 :(得分:0)
insertOrThrow
的第一个参数必须是表名(TABLE_TWEETS
),而不是创建整个表的语句(CREATE_TWEETS_TABLE
)。