我在Sqlite数据库中创建一个表。列为
Sms_Address
字符串类型,Sms_Body
字符串类型,Status_msg_ Integer type. It shows
Java.lang.Runtime异常`。Log cat
02-22 09:55:24.412 10300-10300/com.smsfilter E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smsfilter/com.smsfilter.SpamActivity}: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: create table sms_tbl(sms_addr text not null, sms_body text not null, 1 INTEGER )
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: create table sms_tbl(sms_addr text not null, sms_body text not null, 1 INTEGER )
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.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
at com.smsfilter.DB_Message.onCreate(DB_Message.java:36)
at com.smsfilter.DB_Message.onUpgrade(DB_Message.java:42)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.smsfilter.DB_Message.ViewMessageData(DB_Message.java:57)
at com.smsfilter.SpamActivity.fetchDatabaseMessage(SpamActivity.java:89)
at com.smsfilter.SpamActivity.populateMessageList(SpamActivity.java:61)
at com.smsfilter.SpamActivity.onCreate(SpamActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
查询如下所示
public static final String DB_Query = "create table " + DB_Table1 + "(" +Sms_Address+" text not null, "+Sms_Body+" text not null, "+Status_Msg+" integer)";
Index Out of Bound异常的Log cat
02-22 10:25:55.392 2281-2281/com.smsfilter E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smsfilter/com.smsfilter.SpamActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.smsfilter.SpamActivity.fetchDatabaseMessage(SpamActivity.java:90)
at com.smsfilter.SpamActivity.populateMessageList(SpamActivity.java:61)
at com.smsfilter.SpamActivity.onCreate(SpamActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
它导致此活动
package com.smsfilter;
import android.app.FragmentManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class SpamActivity extends AppCompatActivity {
List<Message> sms;
ArrayList<Message> sms1;
public ArrayList<String> al;
ListView lv;
CheckBox checkbox;
public static String demo;
DB_Message dbmessage = new DB_Message(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messagebox);
lv= (ListView) findViewById(R.id.lvMsg);
checkbox = (CheckBox) findViewById(R.id.checkBox);
sms = new ArrayList<>();
al=new ArrayList<>(0);
populateMessageList(); //Line No. 53
Collections.reverse(sms);
}
public void populateMessageList() {
sms1 = new ArrayList<>();
fetchDatabaseMessage(); //Line No. 61
lv.setAdapter(new datalist(getApplicationContext(), sms1));
// to handle click event on listView item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView textViewSMSSender = (TextView) v.findViewById(R.id.lblNumber);
TextView textViewSMSBody = (TextView) v.findViewById(R.id.lblMsg);
TextView textViewSMSDate = (TextView) v.findViewById(R.id.smsdate);
String smsSender = textViewSMSSender.getText().toString();
String smsBody = textViewSMSBody.getText().toString();
String smsDate = textViewSMSDate.getText().toString();
Intent intentspam = new Intent(getApplicationContext(), DisplaySpamsms.class);
intentspam.putExtra("number",smsSender);
intentspam.putExtra("msg",smsBody);
startActivity(intentspam);
}
});
}
// This method featch the message stored in database
public void fetchDatabaseMessage(){
sms = dbmessage.ViewMessageData();
String addr = sms.get(0).getmAddress(); //Line No 90
if (addr.equals(sms.get(0).getmAddress())) {
for (int i = 0; i < sms.size(); i++) {
al.add(sms.get(i).getmAddress());
//al.add(sms.get(i).getmBody());
}
}else {
System.out.println("SMS Not Displayed");
}
}
class datalist extends BaseAdapter {
Context context;
ArrayList<Message> arrayListsms;
public datalist(Context context, ArrayList<Message> arrayListsms) {
// TODO Auto-generated constructor stub
this.context = context;
this.arrayListsms = arrayListsms;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return sms.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return getItem(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflator = getLayoutInflater();
View row;
row = inflator.inflate(R.layout.row, parent, false);
// ImageView img1 = (ImageView) row.findViewById(R.id.imageView1);
final CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
final TextView txt1 = (TextView) row.findViewById(R.id.lblMsg);
final TextView txt2 = (TextView) row.findViewById(R.id.lblNumber);
final TextView txt3 = (TextView) row.findViewById(R.id.smsdate);
//Long timestamp = Long.parseLong(sms.get(position).getmDate());
Calendar mcalendar = Calendar.getInstance();
//mcalendar.setTimeInMillis(timestamp);
DateFormat mformatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
txt1.setText(sms.get(position).getmBody());
txt2.setText(sms.get(position).getmAddress());
//+"\n"+mformatter.format(mcalendar.getTime())
txt3.setText(mformatter.format(mcalendar.getTime()));
}
final String msgBody = txt1.getText().toString();
final String msgAddr = txt2.getText().toString();
final String msgDate = txt3.getText().toString();
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// selectedStrings.add(tv.getText().toString());
Toast.makeText(context, "" + msgBody, Toast.LENGTH_LONG).show();
System.out.println("Body" + msgBody);
System.out.println("Address" + msgAddr);
System.out.println("Date" + msgDate);
al.add(msgBody + msgAddr + msgDate);
// al.add(msgAddr);
// al.add(msgDate);
/* ContentValues values = new ContentValues();
values.put("address", sms.get(position).getmAddress());
values.put("body", sms.get(position).getmBody());
String date = sms.get(position).getmDate();
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss");
getContentResolver().insert(Uri.parse("content://sms/inbox"), values); */
/* Bundle bundle = new Bundle();
bundle.putString("address",sms.get(position).getmAddress());
bundle.putString("body",sms.get(position).getmBody());
InboxFragment inboxfragment = new InboxFragment();
inboxfragment.setArguments(bundle);
//inboxfragment.setArguments(bundle); */
/* bundle.putString("address", "sms.get(position).getmAddress()");
// set Fragment Arguments
InboxFragment fragment = new InboxFragment();
fragment.setArguments(bundle); */
dbmessage.getStatus();
dbmessage.setStatus();
dbmessage.deleteAll();
sms.remove(position);
notifyDataSetChanged();
} else {
// selectedStrings.remove(tv.getText().toString());
}
}
});
return row;
}
}
}
数据库中的ViewMessageData()函数。
public List<Message> ViewMessageData(){
List<Message> list = new ArrayList<>(0);
SQLiteDatabase sqldb = this.getWritableDatabase();
String select_query = "SELECT * FROM " + DB_Table1 + " where sms_addr = sms_addr";
Cursor cursor = sqldb.rawQuery(select_query,null);
Log.d("sk", "cursor called");
if (cursor.moveToFirst()){
do {
int addr = cursor.getInt(cursor.getColumnIndex("sms_addr"));
Message msg = new Message();
msg.setmAddress(cursor.getString(0));
msg.setmBody(cursor.getString(1));
list.add(msg);
}while (cursor.moveToNext());
Log.d("sm","move 2 next");
}
return list;
}
更新查询执行应用程序时强制关闭。没有这样的列
Status_Msg
。下面的日志cat显示此错误。 记录猫
02-23 06:20:21.713 492-492/com.smsfilter E/AndroidRuntime: FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such column: Status_Msg (code 1): , while compiling: UPDATE sms_tbl SET Status_Msg=? WHERE status
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.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1563)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)
at com.smsfilter.DB_Message.setStatus(DB_Message.java:108)
at com.smsfilter.SpamActivity$datalist$1.onCheckedChanged(SpamActivity.java:205)
at android.widget.CompoundButton.setChecked(CompoundButton.java:126)
at android.widget.CompoundButton.toggle(CompoundButton.java:87)
at android.widget.CompoundButton.performClick(CompoundButton.java:99)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
用于更新状态的SetStatus()函数
public String setStatus(){
SQLiteDatabase sqldb = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Status_Msg",0);
sqldb.update(DB_Table1, values, "" + Status_Msg, null);
System.out.println("" + Status_Msg);
sqldb.close();
return Status_Msg;
}
答案 0 :(得分:0)
更改以下行 -
public static final String DB_Query = "create table " + DB_Table1 + "("
+Sms_Address+" text not null, "+Sms_Body+" text not null, "
+Status_Msg+" integer)";
到 -
String Status_Msg = "status_msg";
public static final String DB_Query = "create table " + DB_Table1 + "("
+Sms_Address+" text not null, "+Sms_Body+" text not null, "
+Status_Msg+" integer default 1)";
答案 1 :(得分:0)
public static final String DATABASE_NAME = "student.db";
public static final int VERSION = 1;
public static final String TABLE_NAME = "tbl_student";
public static final String S_ID = "s_id";
public static final String S_NAME = "s_name";
public static final String S_GENDER = "s_gender";
public static final String CREATE_TABLE = "create table "+TABLE_NAME+"("
+S_ID+ " integer primary key autoincrement, " +S_NAME+ " text not null, " +S_GENDER+ " text not null )";
答案 2 :(得分:-2)
可能需要一个空间“(”。
public static final String DB_Query =“create table”+ DB_Table1 +“(”+ Sms_Address +“text not null,”+ Sms_Body +“text not null,”+ Status_Msg +“integer)”;