E / SQLiteLog:(1)接近" ENTER":语法错误

时间:2016-07-19 20:30:49

标签: java sqlite android-studio android-sqlite

我在尝试创建数据库时遇到了这个奇怪的错误。我正在构建一个费用管理器应用程序,并希望将textview中的数据存储到SQLite数据库中,但是当我按下保存按钮时,我收到了这些错误。 IS这是因为我试图在另一个活动而不是MainActivity中创建数据库?

07-20 01:49:31.032 28795-28795/com.example.alkesh.expensemanager101 E/SQLiteLog: (1) near "ENTER": syntax error
07-20 01:49:31.032 28795-28795/com.example.alkesh.expensemanager101 D/AndroidRuntime: Shutting down VM


                                                                                      --------- beginning of crash
07-20 01:49:31.042 28795-28795/com.example.alkesh.expensemanager101 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.alkesh.expensemanager101, PID: 28795
                                                                                      Theme: themes:{default=overlay:com.baranovgroup.nstyle, iconPack:com.baranovgroup.nstyle, fontPkg:com.baranovgroup.nstyle, com.android.systemui=overlay:com.baranovgroup.nstyle, com.android.systemui.navbar=overlay:com.baranovgroup.nstyle}
                                                                                      android.database.sqlite.SQLiteException: near "ENTER": syntax error (code 1): , while compiling: ENTER INTO money (name) VALUES ('25');
                                                                                          at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                          at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                          at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                          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:1674)
                                                                                          at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                                          at com.example.alkesh.expensemanager101.AddMoney.insertIntoDatabase(AddMoney.java:95)
                                                                                          at com.example.alkesh.expensemanager101.AddMoney.onClick(AddMoney.java:63)
                                                                                          at android.view.View.performClick(View.java:5204)
                                                                                          at android.view.View$PerformClick.run(View.java:21158)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                          at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)

这是我的AddMoney活动代码

package com.example.alkesh.expensemanager101;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddMoney extends AppCompatActivity implements View.OnClickListener{

    Button Save,Cancel;
    EditText expText;
    int[] expense=new int[100];
    int temp=900;
    int c=0;
    int sum=0;
    String DBOnce;

    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_money);

        expText=(EditText)findViewById(R.id.expense);

        Cancel=(Button)findViewById(R.id.cancelbutton);
        Cancel.setOnClickListener(this);

        Save = (Button)findViewById(R.id.savebutton);
        Save.setOnClickListener(this);

        CreateDatabase();
        }





    @Override
    public void onClick(View view) {

        if(view== Save){

            insertIntoDatabase();

        }

        if(view==Cancel){
            Intent intent =new Intent(AddMoney.this,MainActivity.class);
            startActivity(intent);
            finish();
        }

    }

    protected void CreateDatabase(){
        db=openOrCreateDatabase("MoneyDB", Context.MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS money(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR);");
    }



    protected void insertIntoDatabase(){
        String amount = expText.getText().toString().trim();

        if(amount.equals("")){
            Toast.makeText(this,"Please enter your expense or press Cancel to go back",Toast.LENGTH_LONG).show();
            return;
        }

        String query="ENTER INTO money (name) VALUES ('"+amount+"');";
        db.execSQL(query);
        Toast.makeText(this,"Expense Saved",Toast.LENGTH_LONG).show();


    }

}

这是我的MainActivity代码。

package com.example.alkesh.expensemanager101;

import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button Add;
    TextView expense;
    int c=0;
    String DBonce="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Add = (Button) findViewById(R.id.addbutton);
        Add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AddMoney.class);
                intent.putExtra("counter", DBonce);
                startActivity(intent);
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menuhome,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.feedback:{}
            break;

            case R.id.exit: finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

在AddMoney Activity函数中,insertIntoDatabase()将您的查询更改为INSERT