我创建了一个sql数据库,想检查一行是否存在,但我得到一个错误,说一列不存在。我正在按当前月份的名称查找行。有人能告诉我我做错了什么吗。代码如下:
数据库帮助程序1:
package com.carrot.wallet.Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
public class DatabaseHelper1 extends SQLiteOpenHelper {
private static final int version = 1;
private static final String name = "YEAR_DETAILS";
private static final String COL0 = "ID";
private static final String COL1 = "MONTH_YEAR";
private static final String COL2 = "SPENT";
private static final String COL3 = "REMAINING";
private static final String TAG = "Database Helper 1";
public DatabaseHelper1(@Nullable Context context) {
super(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG , "Database 1 is being created");
String create_table = "CREATE TABLE "+name+"("+
COL0+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
COL1+" TEXT, "+
COL2+ "DOUBLE, "+
COL3+" DOUBLE);";
db.execSQL(create_table);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+name);
onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+name);
onCreate(db);
}
private boolean createMonth(String month){
Log.d(TAG , "month row is being created");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1 , month);
contentValues.put(COL2 , 0.0);
contentValues.put(COL3 , 0.0);
long result = db.insert(name,null , contentValues);
return result != -1;
}
public boolean spentChange(String month , Double newAmount){
Log.d(TAG , "spent amount is being changed");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL2 , newAmount);
long result = db.update(name, values , "MONTH_YEAR = ?", new String[]{month});
return result != -1;
}
public boolean remainingChange(String month , Double newAmount){
Log.d(TAG , "Remaining amount is being changed");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues =new ContentValues();
contentValues.put(COL3 , newAmount);
long result = db.update(name , contentValues , "MONTH_YEAR = ?" , new String[]{month});
return result != -1;
}
public void checkExistance(String month){
Log.d(TAG , "Checking if month already exits");
SQLiteDatabase database = this.getReadableDatabase();
Cursor cur = database.query(name , null , "MONTH_YEAR= ?" , new String[]{month} , null , null , null);
if(cur == null) {
boolean b = createMonth(month);
if(b)
Log.d(TAG , "checkExistance: failed to create month");
else
Log.d(TAG, "checkExistance: month created successfully");
}
}
public Cursor getdata(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM YEAR_DEATAILS;";
return db.rawQuery(query , null);
}
}
家庭片段:::
package com.carrot.wallet.ui;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import com.carrot.wallet.AddTransaction;
import com.carrot.wallet.Database.DatabaseHelper1;
import com.carrot.wallet.R;
import com.carrot.wallet.viewPager.CardAdapter;
import com.carrot.wallet.viewPager.CardDetails;
import com.carrot.wallet.viewPager.ZoomOutPageTransformer;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.Calendar;
public class HomeFragment extends Fragment {
private PagerAdapter pagerAdapter;
private ArrayList<CardDetails> cardDetailList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
final FloatingActionButton addTransaction = root.findViewById(R.id.add_transaction_button);
ViewPager viewPager = root.findViewById(R.id.viewpager);
populateArrayList();
pagerAdapter = new CardAdapter(getFragmentManager() , cardDetailList);
viewPager.setAdapter(pagerAdapter);
viewPager.setPageTransformer(false, new ZoomOutPageTransformer());
addTransaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity() , AddTransaction.class));
}
});
return root;
}
private void populateArrayList(){
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
String month_year = month+"-"+year;
DatabaseHelper1 databaseHelper1 = new DatabaseHelper1(getContext());
databaseHelper1.checkExistance(month_year);
Cursor data = databaseHelper1.getdata();
while(data.moveToNext()){
CardDetails cardDetails = new CardDetails(data.getString(1) , data.getDouble(2) , data.getDouble(3));
cardDetailList.add(cardDetails);
}
}
}
错误:
2020-05-28 12:34:44.844 16406-16406/com.carrot.wallet D/Database Helper 1: Checking if month already exits
2020-05-28 12:34:44.846 16406-16406/com.carrot.wallet E/SQLiteLog: (1) no such column: MONTH_YEAR
2020-05-28 12:34:44.847 16406-16406/com.carrot.wallet D/AndroidRuntime: Shutting down VM
2020-05-28 12:34:44.848 16406-16406/com.carrot.wallet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.carrot.wallet, PID: 16406
android.database.sqlite.SQLiteException: no such column: MONTH_YEAR (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM YEAR_DETAILS WHERE MONTH_YEAR = ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1305)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1176)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1344)
at com.carrot.wallet.Database.DatabaseHelper1.checkExistance(DatabaseHelper1.java:82)
at com.carrot.wallet.ui.HomeFragment.populateArrayList(HomeFragment.java:70)
at com.carrot.wallet.ui.HomeFragment.onCreateView(HomeFragment.java:45)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1514)
at android.app.Activity.performStart(Activity.java:7843)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3411)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2122)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7695)
at java.lang.reflect.Method.invoke(Native Method)
2020-05-28 12:34:44.848 16406-16406/com.carrot.wallet E/AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2020-05-28 12:34:46.545 16406-16423/com.carrot.wallet W/System: A resource failed to call close.
1条答案
按热度按时间soat7uwm1#
您有拼写错误select*from year details
此外,你应该添加间距之前=
我建议你改用房间图书馆,这样不容易出错